我正在为流利的有序构造函数创建一个接口,我编写了以下代码:
public interface IMyObjectFactory: IObjectWithProperty
{
int objectId { get; set; }
IObjectWithProperty WithObjectId(int id);
}
public interface IObjectWithProperty
{
IObjectWithProperty WithObjectId(int id);
MyObject Create();
}
第二个接口需要在构造方法中强制执行顺序
暗示是这样的:
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();
但:
- 我必须在构造函数的接口和 impl 中定义属性并且不太喜欢
- 静态 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.
但不是一种新方法,我需要第二个接口来强制执行订单
有什么建议吗?谢谢