简体   繁体   English

验证逻辑软件模式

[英]Validation Logic software Pattern

I have Software Products that can be installed on one or more systems (Servers/ Desktops/ Hardwares) Each product has logic on how it keyCode is defined (specification).我有可以安装在一个或多个系统(服务器/台式机/硬件)上的软件产品,每个产品都有关于如何定义 keyCode(规范)的逻辑。 The logic may be same for many Software Products with little change.对于许多几乎没有变化的软件产品,逻辑可能是相同的。

Product-> defined by ProductCode ->required unique keyCode for installation Product->由ProductCode定义->安装所需的唯一keyCode

I need to validate key entered by a user against the keycode validation for that product.我需要根据该产品的密钥代码验证来验证用户输入的密钥。

What is a good software pattern for this kind of process对于这种过程,什么是好的软件模式

The way I have done so far is到目前为止我所做的方式是

{
    readonly arrayofproductCodes;
    public CanProcess(string productCode)
    {
        return  arrayofproductCodes.contains(productCode);
    }


    public validate(string keyCode)
    {
        return validation result;
    }
}

There could be several 100 Software Products and new ones added every other month.可能会有 100 种软件产品,并且每隔一个月就会添加新的产品。

I feel that there should be some creation pattern to create Instantiate validation logic based on ProductsCode.我觉得应该有一些创建模式来创建基于 ProductsCode 的 Instantiate 验证逻辑。

Let me know if my quetion is clear.让我知道我的问题是否清楚。

Regards,问候,

Mar三月

What you can do is create an interface:您可以做的是创建一个接口:

public interface IProductValidator
{
    bool CanProcess(string productCode);
    void Validate(string keyCode);
}

Then implement this interface for every product you want to validate:然后为您要验证的每个产品实现此接口:

public class XYZProductValidator : IProductValidator
{
     public bool CanProcess(string productCode)
     {
         return productCode == "XYZ";
     }

     public void Validate(string keyCode)
     {
         //validation logic
     }
}

If you're using dependency injection, go and register this as a singleton.如果您正在使用依赖注入,请将其注册为单例。 If you're not using DI, then you need some factory class that will be responsible for the creation of every class.如果您不使用 DI,那么您需要一些工厂类来负责创建每个类。

In your calling code, if you are using DI, you can inject an IEnumerable<IProductValidator> productValidators .在您的调用代码中,如果您使用 DI,您可以注入一个IEnumerable<IProductValidator> productValidators

public class Calling
{
    private readonly IEnumerable<IProductValidator> _productValidators;

    public Calling(IEnumerable<IProductValidator> productValidators)
    {
        _productValidators = productValidators;
    }

    public void Validate(string productCode, string keyCode)
    {
        //find the right validator based on productCode
        var validator = _productValidators.Where(v => v.CanProcess(productCode));
        validator.Validate(keyCode);
    }

}

This way in the future when you add more products, all you have to do is implement the IProductValidator interface and all your calling code will just work.这样以后当您添加更多产品时,您所要做的就是实现IProductValidator接口,您的所有调用代码都会正常工作。 Now, if you're worried about looping through and IEnumerable to find the correct IProductValidator , you can have another class eg ProductValidatorProvider , inject the IEnumerable<IProductValidator> in there, convert that into a Dictionary<string, IProductValidator> and then use that to find the correct IProductValidator.现在,如果您担心遍历IEnumerable以找到正确的IProductValidator ,您可以拥有另一个类,例如ProductValidatorProvider ,在其中注入IEnumerable<IProductValidator> ,将其转换为Dictionary<string, IProductValidator>然后使用它来找到正确的 IProductValidator。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM