我想在我的領域層做一個簡單的規範模式實現。尋找通用的方法來實現基類中的功能
如果我有一個靜態類全規格如下:
public static class FooSpecifications
{
public static Func<Foo, bool> IsSuperhuman
{
get
{
return foo => foo.CanShootLasersOutOfItsEyes && foo.CanFly;
}
}
}
然後,我可以做令人驚異的事情是這樣的:
IEnumerable<Foo> foos = GetAllMyFoos();
var superFoos = foos.Where(FooSpecifications.IsSuperhuman);
我還可以添加布爾方法來富,以確定是否一個特定的例子符合一個規範:
public bool Meets(Func<Foo, bool> specification)
{
return specification.Invoke(this);
}
鑑於Foo,就像我所有的域entiti es,擴展DomainObject,有沒有辦法將Meets()的通用實現放入DomainObject中,以便在每個實體中分別實現Meets()?
提示:您可以使用Predicate而不是Func 。 –
2010-08-19 10:49:51
@Paul:你可以,但是更新版本的框架,特別是LINQ的趨勢是使用'Func'而不是'Predicate '。 –
LukeH
2010-08-19 10:54:56
@LukeH:有趣的,不知道這個(顯然)謝謝。 – 2010-08-19 12:41:53