本文介绍了在.NET验证数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找任何推荐的验证框架,或者一个N层客户端应用程序的模式,我想写的验证方法一次并将其绑定到一个WPF图形用户界面,有可能的话也可用于服务器端和客户端相关业务逻辑。

I'm looking for any recommended validation frameworks, or patterns for an N-tier client application, I want to write the validation method once and bind it to a WPF GUI, and if possible also for server side and client side related business logic.

推荐答案

很简单,你的企业实体应包括部分存储业务验证(即业务逻辑)。然后,这将工作,无论你正在设计一个基于Web的应用程序,基于客户端的应用程序,移动应用程序等,十分灵活。

Very simple, your business entities should include a section to store Business Validation (that is business logic). This will then work whether you are designing a web based app, a client based app, a mobile app, etc. Very flexible.

假设你有一个业务对象(实体),如客户:

Suppose you have a business object (an entity) like a customer:

public class Customer
 {
         public Customer(int customerId, string company, string city, string country)
        {
            CustomerId = customerId;
            Company = company;
            City = city;
            Country = country;
        }
  }

然后你意识到你希望你的业务逻辑声明,您验证将CustomerID(除了它> 0),则需要一个公司的名字(没有一个顾客是无效的),等等等等,这些验证是你的业务逻辑层。所以,你可以改变你的客户类从说一个BusinessObject的层继承,所以你的类成为这样的:

You then realize that you want your business logic to state that you validate a customerID (in addition it is > 0), you require a company name (without one the customer is not valid), etc etc. These validations are your business logic layer. So you can change your customer class to inherit from say a BusinessObject layer, so your class becomes this:

    public class Customer : BusinessObject
    {
        /// <summary>
        /// Default constructor for customer class.
        /// Initializes automatic properties.
        /// </summary>
        public Customer()
        {
            // Default property values
            Orders = new List<Order>();

            // Business rules
            AddRule(new ValidateId("CustomerId"));

            AddRule(new ValidateRequired("Company"));
            AddRule(new ValidateLength("Company", 1, 40));

        }

        /// <summary>
        /// Overloaded constructor for the Customer class.
        /// </summary>
        /// <param name="customerId">Unique Identifier for the Customer.</param>
        /// <param name="company">Name of the Customer.</param>
        /// <param name="city">City where Customer is located.</param>
        /// <param name="country">Country where Customer is located.</param>
        public Customer(int customerId, string company, string city, string country)
            : this()
        {
            CustomerId = customerId;
            Company = company;
            City = city;
            Country = country;
        }
//much more code like methods...
}

您不希望BusinessObject的类型的对象,但什么BusinessObject的作为是存储验证错误和业务规则的抽象类。业务规则是最终当你叫你所说的规则AddRule:

You do not want objects of BusinessObject type but what businessobject serves as is an abstract class that stores validation errors and business rules. A business rule is ultimately the rules you stated when you called AddRule:

public abstract class BusinessObject
{
    /// <summary>
    /// Default value for version number (used in LINQ's optimistic concurrency)
    /// </summary>
    protected static readonly string _versionDefault = "NotSet";

    // List of business rules
    private IList<BusinessRule> _businessRules = new List<BusinessRule>();

    // List of validation errors (following validation failure)
    private IList<string> _validationErrors = new List<string>();

    /// <summary>
    /// Gets list of validations errors.
    /// </summary>
    public IList<string> ValidationErrors
    {
        get { return _validationErrors; }
    }

    /// <summary>
    /// Adds a business rule to the business object.
    /// </summary>
    /// <param name="rule"></param>
    protected void AddRule(BusinessRule rule)
    {
        _businessRules.Add(rule);
    }

    /// <summary>
    /// Determines whether business rules are valid or not.
    /// Creates a list of validation errors when appropriate.
    /// </summary>
    /// <returns></returns>
    public bool Validate()
    {
        bool isValid = true;

        _validationErrors.Clear();

        foreach (BusinessRule rule in _businessRules)
        {
            if (!rule.Validate(this))
            {
                isValid = false;
                _validationErrors.Add(rule.ErrorMessage);
            }
        }
        return isValid;
    }
}

每个业务规则,如validateid,的validate- Required和validatelength需要实现:

Each business rule such as validateid, validaterequired, and validatelength need to be implemented:

public class ValidateId : BusinessRule
{
    public ValidateId(string propertyName)
        : base(propertyName)
    {
        ErrorMessage = propertyName + " is an invalid identifier";
    }

    public ValidateId(string propertyName, string errorMessage)
        : base(propertyName)
    {
        ErrorMessage = errorMessage;
    }

    public override bool Validate(BusinessObject businessObject)
    {
        try
        {
            int id = int.Parse(GetPropertyValue(businessObject).ToString());
            return id >= 0;
        }
        catch
        {
            return false;
        }
    }

     public class ValidateLength : BusinessRule
        {
            private int _min;
            private int _max;

            public ValidateLength(string propertyName, int min, int max)
                : base(propertyName)
            {
                _min = min;
                _max = max;

                ErrorMessage = propertyName + " must be between " + _min + " and " + _max + " characters long.";
            }

            public ValidateLength(string propertyName, string errorMessage, int min, int max)
                : this(propertyName, min, max)
            {
                ErrorMessage = errorMessage;
            }

            public override bool Validate(BusinessObject businessObject)
            {
                int length = GetPropertyValue(businessObject).ToString().Length;
                return length >= _min && length <= _max;
            }
        }

这些仅仅是两个样本validatelength,并validateid,你能想出的validate- Required(检查值存在)。他们都实现业务规则类:

Those are just two samples of validatelength, and validateid, you can come up with validaterequired (check if the value exists). They both implement a business rule class:

public abstract class BusinessRule
    {
        public string PropertyName { get; set; }
        public string ErrorMessage { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="propertyName">The property name to which rule applies.</param>
        public BusinessRule(string propertyName)
        {
            PropertyName = propertyName;
            ErrorMessage = propertyName + " is not valid";
        }

        /// <summary>
        /// Overloaded constructor
        /// </summary>
        /// <param name="propertyName">The property name to which rule applies.</param>
        /// <param name="errorMessage">The error message.</param>
        public BusinessRule(string propertyName, string errorMessage)
            : this(propertyName)
        {
            ErrorMessage = errorMessage;
        }

        /// <summary>
        /// Validation method. To be implemented in derived classes.
        /// </summary>
        /// <param name="businessObject"></param>
        /// <returns></returns>
        public abstract bool Validate(BusinessObject businessObject);

        /// <summary>
        /// Gets value for given business object's property using reflection.
        /// </summary>
        /// <param name="businessObject"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        protected object GetPropertyValue(BusinessObject businessObject)
        {
            return businessObject.GetType().GetProperty(PropertyName).GetValue(businessObject, null);
        }
    }

所以,你的BusinessObject的类只是不断要求业务规则和它捕获的验证错误列表。业务规则类只存储属性的名称,并在情况下,规则是不正确应用的错误消息。它也包含其定义为每个校验类抽象的validate()方法。它不同于针对每个校验类,因为验证一个id是不同的,然后验证必填字段。

So your businessobject class just keeps a list of required business rules and the validation errors it catches. The business rule class simply stores the property name and an error message in case a rule is not correctly applied. It also contains an abstract validate() method which is defined for each validation class. It differs for each validation class because validating an id is different then validation a required field.

四个网站的团伙有很多关于这个好帮手。这在很大程度上code取自他们的模型。

The gang of four website has a lot of good help regarding this. Much of this code is taken from their models.

这篇关于在.NET验证数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 04:30