现在有许多Fluent实现与Lambdas一起完成非常整齐的工作。我想围绕它把自己的大脑包裹起来,这样我就可以开始创造这些东西了,但是我还没有找到我的大脑理解的解释。Lambda Func <>和Fluent
考量一个人验证
public class PersonValidator : IValidator<Person>
{
public PersonValidator()
{
AddRule(p => p.FirstName).CannotBeNull().CannotBeBlank();
AddRule(p => p.LastName).CannotBeNull().CannotBeBlank();
}
public List<ValidationResult> Validate(Person p)
{
// pseudo...
apply all rules specified in constructor, return results
}
}
我设法让这一切使用方法上我的验证这样的工作的一部分...
public ValidationResult<T,TProp> AddRule<T,TProp>(Func<T,TProp> property)
{
... not sure what to do here. This method gives me the ability to use the lambda
... for specifying which properties i want to validate
}
我的这个简单的例子然后可以创建用于CannotBeNull和CannotBeEmpty的扩展IValidator的扩展方法。
所以看起来我有上半年和下半年的问题,但我不知道如何把它们放在一起。
寻找一个有意义的解释...我想“得到它”。 :)
你的例子是没有意义的,当你做AddRule()。CannotBeNull()。CannotBeBlank()你说你想要这些规则添加到您的验证规则,后来申请呢? – 2009-08-21 03:48:48
是的,确切地说。我希望能够使用AddRule,然后使用任何数量的链接方法对类的给定属性应用验证。 我的挑战是我不知道在“AddRule”里面做什么。我知道我需要坚持那些验证者,但我不知道该怎么做? – ctorx 2009-08-21 15:18:42