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 ();}}}
: