Processed in a filter-based manner,
For example: Before submitting an order, do the following:
- No further submissions can be made after 5 seconds after the user submits the order.
- Check the parameters of the order
- To determine whether the order is a heavy demand list
- Make additional parameter updates to the submitted orders, etc.
Similar to the various judgments before adding an order to a database, various logic can be split into one filter, and all of the filter is executed before the real follow-up logic.
This can be done at any time to pull down a logic, or at any time add a logic. Changing one logic does not affect other logic.
It also makes the code more organized.
--------------------------------------------------------------------------------------------------------------- --------------------------------
write a class library that uses only the write Fiilter, and the last business logic code will do.
Class Library already mentions Github (Https://github.com/jinshuai/FilterRunner.Net)
--------------------------------------------------------------------------------------------------------------- ------------------------------
It is customary to go directly to the code:
Use:
namespacedemo{classProgram {Static voidMain (string[] args) { varuser =NewUser () {name="Tom", age= the}; varRunner =NewFilterrunner.runner<user> (User, p ={Console.WriteLine ("Hello"+p.name); return NewFilterrunner.runnermessage () {issuccess=true, message="OK"}; }); //Add FilterRunner. Filters.add (NewCheckuserrunnerfilter<user>()); //Run varresult=Runner. Run (); Console.WriteLine ("-----------"+result. Message+"---------------"); Console.readkey (); } } Internal classUser { Public stringName {Get;Set; } Public ShortAge {Get;Set; } }}
Class Library:
/// <summary> ///Filter/// </summary> Public Interfaceirunnerfilter<inchT> { /// <summary> ///Filter Execution Order/// </summary> intSequence {Get;Set; } /// <summary> ///Filter Execution/// </summary> /// <param name= "Targetobj" ></param> /// <param name= "Iscontinuerunnext" ></param> /// <returns></returns>Runnermessage Run (T targetobj, out BOOLiscontinuerunnext); }
/// <summary> ///return Message/// </summary> Public classRunnermessage {/// <summary> ///whether the execution was successful/// </summary> Public BOOLissuccess {Get;Set;} /// <summary> ///Status Code/// </summary> Public intStatusCode {Get;Set;} /// <summary> ///Execute Message/// </summary> Public stringMessage {Get;Set;} /// <summary> ///Additional Data/// </summary> Publicdictionary<string,Object> Dataext {Get;Set;} }
/// <summary> ///Partial Filter Actuators/// </summary> /// <typeparam name= "T" >The type of image processed</typeparam> Public classRunner<t> { protectedT _targetobj =default(T); //perform a pair of images protected ReadOnlyFunc<t,runnermessage> Executor =NULL; /// <summary> ///Filter Filters/// </summary> PublicList<irunnerfilter<t>> Filters =NewList<irunnerfilter<t>>(); /// <summary> ///Construction/// </summary> /// <param name= "obj" >The pair of images to be processed</param> /// <param name= "Runner" ></param> PublicRunner (T obj, func<t,runnermessage>runner) {_targetobj=obj; Executor=runner; } /// <summary> ///Run/// </summary> /// <returns></returns> Public Virtualrunnermessage Run () {varIscontinuerunnext =true; varresult = Runfilters ( outiscontinuerunnext); if(Iscontinuerunnext = =false) { returnresult; } if(Executor = =NULL) { Throw NewException ("Runner is Null!"); } returnExecutor (_targetobj); } /// <summary> ///Run all the Filter/// </summary> /// <param name= "Iscontinuerunnext" ></param> /// <returns></returns> protected VirtualRunnermessage Runfilters ( out BOOLIscontinuerunnext) {Iscontinuerunnext=true; if(Filters = =NULL|| Filters.count <=0) { return NULL; } runnermessage result=NULL; Filters= Filters.where (Filter = = Filter! =)NULL). (P =p.sequence). ToList (); foreach(varFilterinchFilters) {Result= Filter. Run ( This. _targetobj, outiscontinuerunnext); //If you don't have to run, jump out if(false==Iscontinuerunnext) { returnresult; } } returnresult; } }
OK, just write it down.
Filter-based processing