C # instantiates the BLL business logic layer by reflection

Source: Internet
Author: User

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

Related Article

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.