Jef*_*eff 5 c# fluent-interface
我有以下课程:
public class Fluently
{
public Fluently Is(string lhs)
{
return this;
}
public Fluently Does(string lhs)
{
return this;
}
public Fluently EqualTo(string rhs)
{
return this;
}
public Fluently LessThan(string rhs)
{
return this;
}
public Fluently GreaterThan(string rhs)
{
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
在英语语法中,你不能拥有"等同于某种东西"或"做出比某事更重要的东西"所以我不希望Is.EqualTo和Does.GreaterThan成为可能.有没有办法限制它?
var f = new Fluently();
f.Is("a").GreaterThan("b");
f.Is("a").EqualTo("b"); //grammatically incorrect in English
f.Does("a").GreaterThan("b");
f.Does("a").EqualTo("b"); //grammatically incorrect in English
Run Code Online (Sandbox Code Playgroud)
谢谢!
要强制执行此类事物,您需要多种类型(限制从哪个上下文可用的内容) - 或者至少需要几个接口:
public class Fluently : IFluentlyDoes, IFluentlyIs
{
public IFluentlyIs Is(string lhs)
{
return this;
}
public IFluentlyDoes Does(string lhs)
{
return this;
}
Fluently IFluentlyDoes.EqualTo(string rhs)
{
return this;
}
Fluently IFluentlyIs.LessThan(string rhs)
{
return this;
}
Fluently IFluentlyIs.GreaterThan(string rhs)
{
return this;
}
}
public interface IFluentlyIs
{
Fluently LessThan(string rhs);
Fluently GreaterThan(string rhs);
}
public interface IFluentlyDoes
{ // grammar not included - this is just for illustration!
Fluently EqualTo(string rhs);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
323 次 |
| 最近记录: |