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:
Mix with data entities without extensions, update entities you may lose the attributes you add
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
Inconvenient to migrate, it is too high coupling with modelstate
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> /////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!