Bllfactory Object Uniform Invocation rules
In my framework, all the business class calls are started with the Bllfactory portal, the delivery of business objects can be created, this unified portal can be easy to remember, and reduce code, more importantly, can be very good, such as cache rules, create rules to encapsulate, simplify the code. The creation of the bllfactory is shown below.
Method One:
usingGlobalegrow.toolkit;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;namespaceglobalegrow.bll{/// <summary> ///The factory class that constructs the business class/// </summary> /// <typeparam name= "T" >business Object Type</typeparam> Public classBllfactory<t>whereT:class { Private StaticHashtable Objcache =NewHashtable (); Private Static ObjectSyncRoot =NewObject (); /// <summary> ///Create or get an instance of the corresponding business class from the cache/// </summary> Public StaticT Instance {Get { stringCacheKey =typeof(T). FullName; T BLL=(T) Objcache[cachekey]; if(BLL = =NULL) { Lock(syncRoot) {if(BLL = =NULL) {Assembly assobj= Assembly.Load (typeof(T). Assembly.getname (). Name); Objectobj =assobj.createinstance (CacheKey); BLL= obj asT; Objcache.add (typeof(T). FullName, BLL); } } } returnBLL; } } }}
View Code
Method Two:
usingGlobalegrow.toolkit;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;namespaceglobalegrow.bll{/// <summary> ///The factory class that constructs the business class/// </summary> /// <typeparam name= "T" >business Object Type</typeparam> Public classBllfactory<t>whereT:class { Private Static ObjectSyncRoot =NewObject (); /// <summary> /// /// </summary> Public StaticT Instance {Get { stringCacheKey =typeof(T). FullName; Lock(syncRoot) {T BLL= Reflect<t>. Create (typeof(T). FullName,typeof(T). Assembly.getname (). Name); returnBLL; } } } }}
View Code
The extension method that the reflection needs to use
usingSystem.Collections;usingSystem.Reflection;namespaceglobalegrow.toolkit{ Public classReflect<t>whereT:class { Private StaticHashtable M_objcache =NULL; Public StaticHashtable Objcache {Get { if(M_objcache = =NULL) {M_objcache=NewHashtable (); } returnM_objcache; } } Public StaticT Create (stringSName,stringSfilepath) { returnCreate (SName, Sfilepath,true); } Public StaticT Create (stringSName,stringSfilepath,BOOLBCache) { stringCacheKey = Sfilepath +"."+SName; T ObjType=NULL; if(bCache) {ObjType= (T) Objcache[cachekey];//read from Cache if(!Objcache.containskey (CacheKey)) {Assembly Assobj=createassembly (Sfilepath); Objectobj = Assobj.createinstance (typeof(T). FullName); ObjType=(T) obj; Objcache.add (CacheKey, objType);//Write cache loads an object within the Dal into the cache } } Else{ObjType= (T) createassembly (Sfilepath). CreateInstance (CacheKey);//Reflection Creation } returnObjType; } Public StaticAssembly createassembly (stringSfilepath) {Assembly Assobj=(Assembly) Objcache[sfilepath]; if(Assobj = =NULL) {Assobj=Assembly.Load (Sfilepath); if(!Objcache.containskey (Sfilepath)) {Objcache.add (Sfilepath, assobj);//will be the whole ITDB. DAL. DLL loading Cache } } returnAssobj; } }}
View Code
Call:
var systemconfigbll = Bllfactory<systemconfigbll>. Instance.getdbservicetime ();
View Code
C # instantiates the BLL business logic layer by reflection