当需要不同数量和类型的参数时,如何创建操作委托列表 [英] How to create List of Action Delegates when different number and types of parameters are needed

查看:40
本文介绍了当需要不同数量和类型的参数时,如何创建操作委托列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有大约二十个类的集合,这些类继承自具有抽象Validate方法的基类.当然,每个类都有不同的验证需求,但是规则之间的组合要求是不同的,因此,您可以想象,这会导致大量的代码重复,例如:

We have a set of about two dozen classes that inherit from a base class which has an abstract Validate method. Of course each class has different validation needs but the rules are needed in different combinations between all of them so, as you can imagine, this resulted in a lot of code duplication, as an example:

A类需要规则1、3、6和9
B类需要规则3、4和8
C类需要规则1、8和9
....

Class A needs rules 1, 3, 6, and 9
Class B needs rules 3, 4, and 8
Class C needs rules 1, 8, and 9
....

你得到图片.

因此,我正在考虑做一个简单的重构,并创建一个Validation Decorator,将每个规则封装在一个类中,并创建一个工厂来为每个类创建即席验证组合.

So I was thinking of doing a simple refactoring and create a Validation Decorator in which each rule would be encapsulated in a class and a factory that would create the adhoc validation combination for each class.

然后,我开始认为每个规则是如此简单,以至于与其为装饰器创建所有管道,不如直接将Action委托存储在工厂中,从而填充一个 List(Action(T)),这样每个类都将遍历执行每个委托的列表.

Then I started thinking that each rule is so simple that instead of creating all the plumbing for the decorator I could probably just have Action delegates stored in the factory which would populate a List(Action(T)) so that each class would just iterate through that list executing each delegate.

问题在于,每个规则之间参数的数量和/或类型会有所不同,例如:

The problem is that the number and/or types of parameters vary between each rule, as an example:

规则1需要可为空的DateTime,DateTime和枚举.
规则2需要一个可为空的DateTime和一个DateTime.
规则3需要一个字符串.....

Rule 1 needs a nullable DateTime, a DateTime, and an enum.
Rule 2 needs a nullable DateTime, and a DateTime.
Rule 3 needs a string .....

这是可以实现的事情,还是我不得不选择对Decorator进行编码?

Is this something that can be accomplished or have I not choice but to code the Decorator?

感谢您的想法.

更新:一些规则示例:

if(EndTime.HasValue && StartTime > EndTime)  
     Throw new Exception (...);

if(Status == StatusEnum.Pass && !EndTime.HasValue)  
     Throw new Exception (...);

if(string.IsNullOrEmpty(ProcessName))
     Throw new Exception (...);

if(string.IsNullOrEmpty(ProductName))
   Throw new Exception (...);

if(string.IsNullOrEmpty(CustomerName))
   Throw new Exception (...);

所有涉及的类都具有属性 DateTime?EndTime DateTime StartTime StatusEnum Status string ProcessName .

All the involved classes have the properties DateTime? EndTime, DateTime StartTime, StatusEnum Status, and string ProcessName.

推荐答案

为什么不创建一个名为 IValidationParameter 之类的接口,然后使每个验证规则都具有IValidationParameter的实现.例如

Why not make an interface called something like IValidationParameter Then have each validation rule have an implementation of IValidationParameter. For example

public class Rule1ValidationParameter : IValidationParameter
{
    public DateTime? FirstDate { get; set; }
    public DateTime SecondDate { get; set; }
    pulbic MyEnum MyEnum { get; set; }
}

然后创建您的列表 List< Action< IValidationParameter>>

这篇关于当需要不同数量和类型的参数时,如何创建操作委托列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆