The actual usage of the EF schema--fluentvalidation

Source: Internet
Author: User
Back to Catalog

In the MVC,EF,LINQ environment, we often use Datamodel (do) and ViewModel (VO), perhaps for their properties to verify that we will adopt the characteristics of the way, of course, this is very intuitive, even Microsoft's demo is like, generally this code

        <summary>///Agency id///</summary>[displayname ("agency ID")]public int Agentid {get; set;} <summary>///Institution name///</summary>[displayname ("Institution name")]        [MaxLength (+)]public string AgentName { Get Set }///<summary>///Agency head///</summary>[displayname ("agency leader")]        [MaxLength (+)]public string agentuser {get; set;}

And this design way to our future maintenance brings a lot of problems, concrete uncle summed up:

    1. Mix with data entities without extensions, update entities you may lose the attributes you add

    2. If there is more than one VO, then you need to add it to the specific VO, because the semantics of do may not be very clear

    3. Inconvenient to migrate, it is too high coupling with modelstate

    4. From an object-oriented point of view, its responsibilities are too singular to cause too many causes of change.

To sum up, Fluentvalidation was born!

NuGet up to install it: Install-package fluentvalidation

One of your entity classes, you can add multiple test classes, which is equivalent to a variety of test classes to decorate an entity class, I think very good!

   public class Createusereventvalidator:abstractvalidator<createuserevent>{public Createusereventvalidator ()        {            rulefor (command = command). UserName). Notempty (). Length (5, 20). Withmessage ("User name ascending is 5-20 characters! ");            Rulefor (Command = command). Email). Notempty (). EmailAddress (). Withmessage ("Not a valid email! ");            Rulefor (Command = command). BirthDay). Notempty (). Must (i = i < DateTime.Now). Withmessage ("Your century is too small!") ");        }    }

When using, you can get the information you need through the properties of isvalid,errors, of course, you can also use it in command events, domain events, such as to do a verification of the adorner, which processing procedures to use the calibration, through this decorator decoration on the line, quite elegant!

      BusManager.Instance.Subscribe (Validatordecorator<createuserevent> (Usereventhandler (),   BusManager.Instance.Subscribe (loggerdecorator<createuserevent> (createuserevent {UserName =  });

Adorners require you to turn a decorative object and an adorner on it.

    <summary>///validation Adorner///</summary>///<typeparam name= "tevent" ></typeparam> [Serializable ]public class Validatordecorator<tevent>: Ibushandler<tevent>where tevent:ibusdata {//<summary&gt /////To be decorated///</summary>private readonly ibushandler<tevent> _inner;///<summary>///Check Adorner set// </summary>private readonly ivalidator<tevent>[] _validators;///<summary>///Initialize//</summary >///<param name= "inner" > Handlers to be decorated </param>///<param name= "validators" > Adorners </param>            Public Validatordecorator (ibushandler<tevent> inner, params ivalidator<tevent>[] validators) {            _inner = inner;        _validators = validators; }public void Handle (tevent evt) {var failures = _validators. Select (v = v.validate (evt)). SelectMany (result = result). Errors). Where (Error = = Error = null). ToList (); if (failures.            Any ()) {throw new validationexception ("Entity checksum failed", failures); } _inner.        Handle (EVT); }    }

For a knowledge of learning and understanding is a need for some theoretical basis, we can look at the design module, algorithm introduction, NETCLR and other books!

Thank you for reading!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.