Footprint: record a rebuild to separate rule production from rule consumption (Execution delegate)

Source: Internet
Author: User

Before reconstruction:

 
Cache. add (dxcachekey. a, register (dxcachekey. a, dxcachekeytype. a, Dal. getitema, Dal. getitemabyid); cache. add (dxcachekey. b, register (dxcachekey. b, dxcachekeytype. b, Dal. getitemb, Dal. getitembbyid ));......

The goal of this refactoring is to separate the establishment and use of rules (idxcacheitemwrapper). The by-product is to encapsulate a certain form of rules (dxcacheitemwrapper ).

Advantages of separation:

First, enhancedCodeReadability. Because this is a natural process, first create a rule and then use it.

Second, other rules can be added to computing tasks as long as they comply with idxcacheitemwrapper.

Third, after the rules are established, you do not need to use them immediately, so you can delay the calculation. During this period, you can filter and add rules.

 Public  Class  Class1
{
Public Class1 ()
{
Dal dal = New Dal ();
List < Idxcacheitemwrapper > List = New List < Idxcacheitemwrapper >
{
New Dxcacheitemwrapper < Itema, Int > (Dxcachekey. A, dxcachekeytype. A, Dal. getitema, Dal. getitemabyid ),
New Dxcacheitemwrapper < Itemb, Int > (Dxcachekey. B, dxcachekeytype. B, Dal. getitemb, Dal. getitembbyid ),
New Dxcacheitemwrapper < Itemc, Int > (Dxcachekey. C, dxcachekeytype. C, Dal. getitemc, Dal. getitemcbyid ),
};

Foreach (Idxcacheitemwrapper W In List)
{
VaR value = W. getvalue ();
Cache. Add (W. Key. tostring (), value );
}
}

}


Public Interface Idxcacheitemwrapper
{
Dxcachekey key { Get ; Set ;}

Object Getvalue ();
}

Public Class Dxcacheitemwrapper < T1, T2 > : Idxcacheitemwrapper
{
Public Dxcacheitemwrapper (dxcachekey key, dxcachekeytype keytype, func < T1 > Getdata1, func < T2, T1 > Getdata2)
{
Key = Key;
Keytype = Keytype;
Getdata1 = Getdata1;
Getdata2 = Getdata2;
}

Public Object Getvalue ()
{
Return Getvalue < T1, T2 > (Key, keytype, getdata1, getdata2 );
}

Public T1 getvalue < T1, T2 > (Dxcachekey key, dxcachekeytype keytype, func < T1 > F1, func < T2, T1 > F2)
{
Return Default (T1 );
}

Public Dxcachekey key { Get ; Set ;}
Public Dxcachekeytype keytype { Get ; Set ;}
Public Func < T1 > Getdata1 { Get ; Set ;}
Public Func < T2, T1 > Getdata2 { Get ; Set ;}
}

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.