. NET Abstract Factory model micro-understanding-how to implement abstract factory in projects

Source: Internet
Author: User

Recently I have been studying MVC. Some projects in MVC are related to the abstract mode. Today I will explain my understanding of the abstract factory mode, to facilitate understanding of MVC and factory models. Please make sure that you are correct! First of all, I will briefly describe the relationship between the three layers. I believe the bloggers who have watched this article are familiar with the three-layer design. Here I will briefly describe the user interface presentation layer (Web) business logic layer (BLL) data access layer (DAL) 1: Data access layer: mainly the operation layer for raw data (in the form of databases or text files that store data, instead of raw data, it refers to data operations, rather than databases, which provide data services for the business logic layer or presentation layer. 2: business logic layer: This layer is mainly used to address specific problems. It can also be understood as a pair of operations on the data layer. If the data layer is a building block, the logical layer is the construction of these blocks. 3: Presentation Layer: It mainly indicates the WEB mode or WINFORM mode. The WEB mode can also be represented as aspx. If the logic layer is quite powerful and complete, the logic layer provides complete services regardless of how the presentation layer is defined and changed. The following describes the functions of the abstract factory model. For more information, see the figure (this figure is drawn by one of my teachers. Here I will give you a brief look, let's follow the project to create an abstract factory model demo and create a solution: for example, the description and reference relationships between them are as follows: AXCModel layer: AXCDAL layer of the object class layer: data access layer IAXCDAL layer: data access layer interface AXCFactoryDAL layer: Data Access factory design AXCBLL layer: business logic layer IAXCBLL layer: business logic layer interface AXCFactory layer: business logic layer factory design AXCWeb layer: Reference relationships between presentation layer interfaces are as follows: the IAXCDAL layer and the IAXCBLL layer reference the reference between the AXCModel layer AXCDAL layer and the AXCModel layer AXCBLL layer reference the reference between the AXCFactoryDAL layer and the IAXCBLL layer and the IAXCDAL layer Factory: the AXCFactoryDAL layer references the AXCDAL layer and the IAXCDAL layer. The AXCFactory layer references the AXCBLL layer and the IAXCBLL layer: the AXCWeb layer references the AXCFactory layer, the IAXCBLL layer, and the AXCModel layer. Below we use a simple function to obtain the number of records to implement the abstract factory mode: Create a PeopleModel class in the AXCModel layer, attributes (automatic attributes) are as follows: 1234567 public class PeopleModel {public int Id {get; set;} public string Name {get; set;} public int Age {get; set ;} public bool Sex {get; set ;}} creates an IPeopleDAL class in the IAXCDAL layer. The content of this class is the interface method in the AXCDAL data access layer, as follows: 1234 public interface IPeopleDAL {List <AXCModel. peopleModel> getPeopleList ();} creates a PeopleDAL class in the AXCDAL layer and inherits the IPeopleDAL class. The content of this class is the implementation of specific methods in the IAXCDAL layer, as shown in the following figure (because no database is designed, manually Replace It): 12345678910111213 public class implements ledal: IAXCDAL. IPeopleDAL {public List <AXCModel. peopleModel> getPeopleList () {List <AXCModel. peopleModel> People = new List <AXCModel. peopleModel> () {new AXCModel. peopleModel () {Id = 1, Name = "Ai xincao", Age = 25, Sex = false}, new AXCModel. peopleModel () {Id = 2, Name = "Ai xincao 1", Age = 23, Sex = true}, new AXCModel. peopleModel () {Id = 3, Name = "Ai xincao 2", Age = 22, Sex = false }}; return People ;}} create an abstract class in the AXCFactoryDAL layer: absFactoryDAL implements the data access layer for reading configurations in the configuration file to facilitate multi-database development, as shown below, 123456789101112131415161718192021 public abstract class AbsFactoryDAL {/// <summary> /// obtain the factory object of the object data based on the configuration file /// </summary> /// <returns> </returns> public static AbsFactoryDAL GetFatory () {string strType = System. configuration. configurationManager. appSettings ["dalType"]. toString (); AbsFactoryDAL dalFactory = null; switch (strType) {case "dal": dalFactory = new DALFatory (); break;} return dalFactory;} public abstract IAXCDAL. IPeopleDAL GetPeople ();} creates a DALFatory class that inherits AbsFactoryDAL and returns a specific data access layer instance, as follows: copy the code public class DALFatory: AbsFactoryDAL {public override IAXCDAL. IPeopleDAL GetPeople () {return new AXCDAL. peopleDAL () ;}} copy the code to create an IPeopleBLL class in the IAXCBLL layer. The content of this class is the interface method in the AXCBLL business logic layer, as shown below: public interface IPeopleBLL {List <AXCModel. peopleModel> getPeopleList ();} creates a PeopleBLL class in the AXCBLL layer and inherits the IPeopleBLL class. The content of this class is the business implementation of the specific methods in the IAXCBLL layer, as follows: copy the public class PeopleBLL: IAXCBLL. IPeopleBLL {// IAXCDAL. IPeopleDAL IPeopleDal = null; // <summary> // data abstraction factory-created by the static method of the abstraction factory class: parent class obj = new subclass (); /// </summary> AXCFactoryDAL. absFactoryDAL absFactoryDAL = AXCFactoryDAL. absFactoryDAL. getFatory (); public List <AXCModel. peopleModel> getPeopleList () {IAXCDAL. IPeopleDAL IPeopleDal = absFactoryDAL. getPeople (); return IPeopleDal. getPeopleList () ;}} the code is similar to creating a factory in the AXCFactoryDAL layer. In the AXCFactory layer, a new abstract class is created: AbsFactoryBLL, which implements the business logic layer for reading configurations in the configuration file, this facilitates the development of multiple business logic layers for multiple databases, as shown below, copy the code /// <summary> /// obtain the entity business factory object based on the configuration file /// </summary> /// <returns> </returns> public static AbsFactoryBLL GetFatory () {string strType = System. configuration. configurationManager. appSettings ["bllType"]. toString (); AbsFactoryBLL bllFactory = null; switch (strType) {case "bll": bllFactory = new BLLFatory (); break;} return bllFactory;} public abstract IAXCBLL. IPeopleBLL GetPeople ();} copy the code to create a new BLLFatory class, which inherits AbsFactoryBLL and returns a specific business logic layer instance, as shown below: copy the code public class BLLFatory: absFactoryBLL {public override IAXCBLL. IPeopleBLL GetPeople () {return new AXCBLL. peopleBLL () ;}} the code has been copied. The basic work is almost done. Now configure the webconfig file in the web layer, as shown below: <appSettings> <add key = "bllType" value = "bll"/> <add key = "dalType" value = "dal"/> </appSettings>

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.