3

我希望能够收集多个验证实体,它们都共享相同的界面。这就是我想出的:

public interface Ivalidateable
{
  bool IsValid(IValidateParam param);
}

public interface IValidateParam
{

}

public abstract EmployeeStrategy: Ivalidateable
{
  public abstract bool IsValid(User user); 
}

public abstract SpreadStrategy: IValidateable
{
 public abstract bool IsAvlid(Campaign campaign);
}

public class User: IValidateParam
{}

public class Campaign: IValidateParam
{}

public EmployeeTypeStragtegy: EmployeeStrategy, IValidateable
{
 public bool IsValid(User user)
 {
  if  (new[]{'e',a','b}.Contains(user.userId.first().toString()))
  return true;

  return false; 
 }
}

public TrailSpreadStrategy: SpreadStrategy, IValidateable
{
 public bool IsValid(Campaign campaign)
 {
  //logic goes here
 }
}

public EvenpreadStrategy: SpreadStrategy:, IValidateable
{
 public bool IsValid(Campaign campaign)
 {
  //logic goes here
 }
}

public class ValidationFactory
{
 private static List<IValidateable> stragtegies;

 static ValidationFactory
 {
  strategies = new List<IValidateable>();
  strategies.Add(new EmployeeTypeStragtegy());
  strategies.Add(new TrailSpreadStrategy());
 }

 public bool IsValid(//Need to pass User/Campaign)
 {
  //what do I do here?
 }
}

传递用户/活动以便我可以循环策略并检查 IsValid 的最佳方法是什么?

4

3 回答 3

3

这是我的做法:

// campaign object class, and it's separate validator logic

public class Campaign
{
}

public class CampaignValidator : IValidationStrategy<Campaign>
{
    public bool IsValid(Campaign test)
    {
        return true;
    }
}

// framework stuff

public interface IValidationStrategy<T>
{
    bool IsValid(T test);
}

public static class ValidationFactory
{
    private readonly static Dictionary<Type, object> _typeValidators;

    static ValidationFactory()
    {
        _typeValidators = new Dictionary<Type, object>();
        _typeValidators[typeof(Campaign)] = new CampaignValidator();
    }

    public static bool IsValid<T>(T obj)
    {
        return ((IValidationStrategy<T>)_typeValidators[typeof(T)]).IsValid(obj);
    }
}

之后,您可以调用ValidationFactory.IsValid(myObject);,如果类型在工厂静态构造函数中,它将为您提供真/假。

验证工厂正在做一种 DI/IOC 的事情,即您可以使用 Ninject、Castle 等来实现将类型映射到验证器类型。

您可以动态地或以不同的方式做事,而不必以相同的方式构建字典 - 查看验证器实现的接口的泛型类型参数,或搜索IValidationStrategy<>基于 - 的类并再次查看泛型类型参数。但是上面的方法又快又简单。

您是否有理由需要单独的验证器类?

于 2012-09-05T21:51:57.197 回答
1

我建议看看FluentValidation的嵌套和集合验证功能。如果没有额外的工厂和接口,这应该可以正常工作 - 如果您的策略包含对您想要与它们一起验证的其他实体的引用。

于 2012-09-05T21:50:21.200 回答
0

这个怎么样?

更改您的所有 IsValid 以使用此原型(用户用户、营销活动),例如:

public EmployeeTypeStragtegy: EmployeeStrategy, IValidateable
{
 public bool IsValid(User user, Campaign campaign)
 {
    return (new char[]{'e','a','b'}.Contains(user.userId.first().toString());
 }
}

然后按适当的类型处理每个,这样它们都接受相同的参数。进而:

public bool IsValid(User user, Campaign campaign)
 {
   bool valid;

   foreach (IValidateable iv in strategies)
   {
      if (!iv.IsValid(user, campaign))
          valid = false;
   }

   // This returns true if all are valid, false if not (not sure if this is the logic you expect)
   return valid;
 }
于 2012-09-05T21:25:54.650 回答