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 ;}
}