1 What is abstract Factory mode
Abstract Factory: Provides an interface to create a series of interrelated or interdependent objects without having to define their specific classes. The structure diagram is as follows:
Analysis of the abstract factory model, 1 provides a series of mutually dependent objects to create the work 2 encapsulate object general Creation Method 3 provides a uniform way to invoke data access 4 avoid invoking data access methods and specific creation work. For a detailed analysis of the above figure:
Abstractproducta and ABSTRACTPRODUCTB are two abstract products that are abstract because they are likely to have two different implementations, while Producta1,producta2 and ProductB1, ProductB2 is the implementation of a specific classification of two abstract products, such as when accessing data, ProductA1 can be likened to accessing a table for SQL Server, and ProductB1 can access the table for access.
Abstractfactory is an abstract factory class or an abstract factory interface, which should contain abstract methods for all products. and ConcreteFactory1 and ConcreteFactory2 are concrete factories, just like Sqlserverfactory and Accessfactory.
Typically, when you run to create an instance of the Concretefactory class, this creates a product object that has a specific implementation, that is, to create a different product object, the client should use a different specific factory. 2 Duties of abstract factories
Through the detailed analysis above, we can get the main function of each object in the abstract factory model, and the responsibility
1. Production of abstract products with abstract factories
2. Production of physical products with entity factories
3. Provide entity product access interface with abstract products
4. Implement your own function with the entity product 3 Abstract Factory mode to achieve the data access layer of three-tier architecture
1 construction projects, such as figure
2 dependencies of the project, adding references based on dependencies (if the references are not added, we recommend that you learn the basics before you learn design patterns)
3 Implement data Access interface
3.1 Definition interface: IAdminService.cs, the code is as follows:
Public interface Iadminservice
{
//detect if the logged-on user exists a
bool checkadmin (int id,string pwd);
}
3.2 Implementation interface: Divides the implementation of the interface into three types of SQL Server, Access, and Oracle.
Only the implementation of SQL Server is introduced, and the other code at the end of the article is attached with the download address of this example source code.
public class Adminservice:iadminservice {#region Iadminservice member private String connstr = @ "Data Source=sunlike\sqlexpress;initial Catalog=blank; Persist Security info=true; User Id=sa;
Password=sa ";
Private SqlConnection conn = new SqlConnection (); private void Open () {if (conn). state = = connectionstate.closed) {Conn.
ConnectionString = ConnStr; Conn.
Open ();
Return } if (conn.
state = = ConnectionState.Open) {return;
} public bool Checkadmin (int ID, string pwd) {open ();
SqlCommand cmd = new SqlCommand (); Cmd.
Connection = conn;
Cmd.commandtype = CommandType.Text;
Cmd.commandtext = "SELECT * from Guanli where username = '" +id+ "' and password = '" +pwd + ";"; Object result = cmd.
ExecuteScalar (); IF (Conn. state = = ConnectionState.Open) Conn.
Close (); Return result!= null?
True:false; } #endregion}
4 Implement data Access Layer object creation function
4.1 Encoding implementation: Provides an abstract method for creating data access objects in an abstract factory (abstractdalfactory).
Public abstract Iadminservice Createadminservice ()//abstract Factory provides abstract products
4.2 Select the Entity factory, no use to reflection, in fact, should be used to reflect, here using simple switch statements, easy to understand, will be attached to the reflection of the code, please pay attention to
public static Abstractdalfactory Choosefactory ()
{
string dbType = configurationmanager.appsettings["DbType" ]. ToString ();
Abstractdalfactory factory = null;
Switch (dbType)
{case
"SQL":
factory = new Sqldalfactory ();
break;
Case "Access":
factory = new Accessdalfactory ();
break;
Case "Oracle":
factory = new Oracledalfactory ();
break;
return factory;
}
5 through the Entity factory to achieve the creation of data access objects, here only give the code of SQL Server, other code see the end of the article download the source of the connection address, thank you
public class Sqldalfactory:abstractdalfactory
{public
override Iadminservice Createadminservice ()
{ return
new Adminservice ();
}
6 The Business Logic Layer (BLL) invokes the data Access layer method
public class Adminhandle
{
//select entity Factory
private static Abstractdalfactory factory = Abstractdalfactory.choosefactory ();
Create a data Access object
private static Iadminservice Adminservice = Factory. Createadminservice ();
Public bool Checkadmin (admin admin)
{return
adminservice.checkadmin (admin.id,admin). Password);
}
#region Other methods
#endregion
}
7 The test code in the user interface layer (UIL), as follows
Admin admin = new Admin (int. Parse (Tbname.text), tbpwd.text);
BOOL IsOK = false;
Adminhandle adminhandle = new Adminhandle ();
IsOK = adminhandle.checkadmin (admin);
if (IsOK)
MessageBox.Show ("Login Successful");
else
MessageBox.Show ("Login Failed");
8 to introduce the system architecture of the three-tier architecture
Data access layer: Dalfactory,idal,dal Three projects, business logic layer: Model and BLL two projects, presentation layer: UIL a project.
The DAL of the data access layer can access other databases such as SQL Server, Access, Oracle, and so on. 4 The advantages and disadvantages of the abstract factory model
Advantages: Easy to exchange product family, if the replacement of the database, only in the app.config configuration file <add key= "DBType" value= "Access"/> Slightly modified, you can complete the replacement of the database, rather than the code.
Disadvantages: If the project needs to replace, add a data table, you need to add three classes in the corresponding DAL, idal add an interface, Idal three entity factory add corresponding three functions, BLL add a class, model add an entity, change relatively large. 5 Abstract Factory mode to achieve the three-tier architecture of the data Access layer source code
Description, download the resources to collect your 1 resources, is a respect for me and the fruits of my labor return it, thank you for your download, but also thank you for your visit, if you have questions, please leave a message, appreciate.
Download Link: http://download.csdn.net/source/3490633