Filter-based processing

Source: Internet
Author: User

Processed in a filter-based manner,

For example: Before submitting an order, do the following:

    1. No further submissions can be made after 5 seconds after the user submits the order.
    2. Check the parameters of the order
    3. To determine whether the order is a heavy demand list
    4. 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

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.