Lambda Func <>和Fluent

cto*_*orx 7 c# lambda fluent-interface

现在有许多Fluent实现可以与Lambdas一起完成非常整洁的工作.我想把我的大脑包裹起来,这样我就可以开始创造一些这样的东西,但我还没有找到一个我的大脑能够理解的解释.

考虑这个简单的Person Validator示例

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
     }
}
Run Code Online (Sandbox Code Playgroud)

我已经成功地使用我的Validator上的方法来完成所有这些工作的一部分......

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
}
Run Code Online (Sandbox Code Playgroud)

然后,我可以创建扩展IValidator的Extension方法,以实现CannotBeNull和CannotBeEmpty的目的.

所以我似乎有问题的上半部分和后半部分,但我不确定如何将它们组合在一起.

寻找有意义的解释...我想"得到它".:)

jri*_*sta 5

流畅接口的关键是像CannotBeNull()和CannotBeBlank()这样的方法返回当前实例(即this).如果您希望AddRule方法"流畅",而不是返回ValidationResult,则需要返回当前的IValidator实例.您的扩展方法还需要返回它们正在扩展的IValidator实例.

我认为你的确切实现可能需要更复杂一点,希望下面的例子能提供一些见解.同样的一般规则,但是......返回"this"以创建一个流畅的界面:

interface IValidator<T>
{
    IValidatorRule<T, TProp> AddRule<TProp>(Func<T, TProp> property);
}

interface IValidatorRule<T>
{
    T instance { get; }
    string PropertyName { get; }

    ValidationResult Apply(T instance);
}

public static IValidatorAugmentorExtensions
{
    public static IValidatorRule<T> CannotBeNull(this IValidatorRule<T> rule)
    {
        // ...

        return rule;
    }

    public static IValidatorRule<T> CannotBeBlank(this IValidatorRule<T> rule)
    {
        // ...

        return rule;
    }
}
Run Code Online (Sandbox Code Playgroud)

以上可以像这样使用:

public class PersonValidator: IValidator<Person>
{
    public PersonValidator()
    {
        AddRule(p => p.FirstName).CannotBeNull().CannotBeEmpty();
        AddRule(p => p.LastName).CannotBeNull().CannotBeEmpty();
    }    

    public List<ValidationResult> Validate(Person p)
    {
        List<ValidationResult> results = new List<ValidationResult>();

        foreach (IValidatorRule<Person> rule in rules) // don't know where rules is, or what the AddRule method adds to...you'll need to figure that out
        {
            results = rule.Apply(p);
        }

        return results;
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然上面演示了如何创建一个流畅的界面,但从长远来看,我并不知道它在这种特殊情况下会给你带来什么.为了方便流畅的界面,似乎只在内部使用具体的验证器,您已经大大增加了代码的复杂性,而没有真正为验证器的使用者提供有用的,流畅的界面.我认为通过为需要执行验证的开发人员提供流畅的验证框架,而不是提供用于创建具体验证器的流畅框架,您将获得更多价值.