IAM創建用於流利的接口有序構造,我已經寫了這個代碼:接口用於流利有序構造
public interface IMyObjectFactory: IObjectWithProperty
{
int objectId { get; set; }
IObjectWithProperty WithObjectId(int id);
}
public interface IObjectWithProperty
{
IObjectWithProperty WithObjectId(int id);
MyObject Create();
}
第二接口是需要在構造方法來執行順序
的IMPL是這:
public class MyObjectFactory: IMyObjectFactory
{
public int objectId { get; set; }
private MyObjectFactory() { }
public static IObjectWithProperty BeginCreation()
{
return new ObjectFactory();
}
public IObjectWithProperty WithObjectId(int id)
{
objectId = id;
return this;
}
public MyObject Create()
{
return new MyObject(this);
}
}
這是我的目標:
public class MyObject
{
public int Id { get; set; }
public MyObject(IMyObjectFactory factory)
{
this.Id = factory.objectId;
}
}
,所以我可以寫
MyObject mo = MyObjectFactory.BeginCreation().WithObjectId(1).Create();
但:
- 我都在構造函數的接口並實現了一套界定財產,不喜歡太多
- 靜態BeginCreation方法沒有接口
- 我必須設置公開impl屬性,而我想它內部
- 在接口我得到這個警告:
Warning 7 'FunzIA.DL.Factory.Interfaces.IMyObjectFactory.WithObjectId(int)' hides inherited member 'FunzIA.DL.Factory.Interfaces.IObjectWithProperty.WithObjectId(int)'. Use the new keyword if hiding was intended.
但不是一個新的方法,我需要第二接口執行順序
任何建議?由於
爲什麼很激動你需要'WithObjectId'中的同樣的方法?你通過繼承來獲得它。 – meilke
排除此用途:MyObjectFactory。WithObjectId(1),它強制您必須先調用BeginCreation() –