Simplified Version: upgrade from simple factory mode to abstract factory mode (reflection is used ).

Source: Internet
Author: User

Simplified Version: upgrade from simple factory mode to abstract factory mode (reflection is used ).

The previous two models have learned how to change the simple three-tier mode to the simple factory mode, but the simple factory mode still has a disadvantage, that is, the simple factory integrates the creation of all instances. Not very good either.

Now we want to use the Abstract Factory method to achieve this:

We add the following code to the Assembly:

<Deleetask> <! -- Namespace --> <add key = "DALNameSpace" value = "DAL"/> <! -- Assembly --> <add key = "DALAssembly" value = "DAL"/> </appSettings>

Create an abstract factory class:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. reflection; using System. configuration; using IDAL; namespace DALFactory {public class DALAbstractFactory {// <summary> /// namespace /// </summary> public static string DALNameSpace {get {return ConfigurationManager. deleetask[ "DALNameSpace"] ;}/// <summary >/// Assembly /// </summary> public static string DALAssembly {get {return ConfigurationManager. deleetask[ "DALAssembly"] ;}} public IClassDAL GetClassInstance () {string fullNameSpace = DALNameSpace + ". classDAL "; return CreateInstance (fullNameSpace, DALAssembly) as IDAL. IClassDAL ;} /// <summary> /// create an instance /// </summary> /// <param name = "fullClassNameSpace"> </param> /// <param name = "assembly"> </param> public static object CreateInstance (string fullClassNameSpace, string assembly) {var DALAssembly = Assembly. load (assembly); return DALAssembly. createInstance (fullClassNameSpace );}}}

The above method creates an instance of the DAL data access layer through reflection.

At the business layer, we can:

Using DAL; using Entity; using IDAL; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace BLL {public class ClassBLL {// coupling is too high // ClassDAL dal = new ClassDAL (); // coupling still exists, the coupling between the business layer and the data access layer is too high // IClassDAL dal = new ClassDAL (); // introduces the simple factory mode // IClassDAL dal = DALFactory. DALFactory. getClassInstance (); // introduce the abstract factory IClassDAL dal = DALFactory. dalw.actfactory. getClassInstance (); /// <summary> /// obtain the Class List /// </summary> /// <returns> </returns> public List <ClassEntity> GetList () {return dal. getList ();}}}

:

 

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.