Petshop4.0 learning Summary-database access Layer Structure Analysis

Source: Internet
Author: User

Address: http://www.cnblogs.com/linecheng/archive/2011/12/15/2289254.html

 

Recently I have been reading petshop4.0 and have been familiar with some database layer designs.

It's not very complicated. It mainly uses a factory and an IOC injection.

The class diagram I drew is as follows (not very standard, my UML water products are average ...)

Among them, Web. config is my own day, mainly to illustrate the IOC problem.

The model mainly defines some entity classes.

Idal provides the abstraction of the database access layer, which is implemented by sqldal and oracledal respectively.

Dalfactory is a reflection factory. It reads the configuration in the configuration file, judges the Dal used, and generates the corresponding idal instance using reflection.

DalfactoryCodeAs follows:

  Public   Sealed   Class Dataaccess {

// Look up the Dal implementation we shoshould be using
Private Static Readonly String Path = configurationmanager. appsettings [ " Webdal " ];
Private Static Readonly String Orderpath = configurationmanager. appsettings [ " Ordersdal " ];

Private Dataaccess (){}

Public Static Petshop. idal. icategory createcategory (){
String Classname = path + " . Category " ;
Return (Petshop. idal. icategory) Assembly. Load (PATH). createinstance (classname );
}

Public Static Petshop. idal. iinventory createinventory (){
String Classname = path + " . Inventory " ;
Return (Petshop. idal. iinventory) Assembly. Load (PATH). createinstance (classname );
}

Public Static Petshop. idal. iItem createitem (){
String Classname = path + " . Item " ;
Return (Petshop. idal. iItem) Assembly. Load (PATH). createinstance (classname );
}

Public Static Petshop. idal. iorder createorder (){
String Classname = orderpath + " . Order " ;
Return (Petshop. idal. iorder) Assembly. Load (orderpath). createinstance (classname );
}

Public Static Petshop. idal. iproduct createproduct (){
String Classname = path + " . Product " ;
Return (Petshop. idal. iproduct) Assembly. Load (PATH). createinstance (classname );
}

}
Copy code

However, all implementations in the dal are configured here.

If we need to change to Oracle database, we only need to change the configuration of the corresponding Dal service object. Because both Dal layers implement idal. If we want to expand access later, we only need to implement the idal, compile and generate the DLL, and then change the configuration file.

A design concept embodied in this dependency injection is that high-level components should depend on the image, rather than a specific function.

The high-level component here is only the business logic layer (BLL) in petshop ).

Dependency injection can not only inject instance objects, but also inject methods and attributes. (Think about the service that can be dynamically generated using reflection)

There are three types of dependency injection: interface-based. The second is based on the property setter, and the third is based on the constructor. Petshop is an implementation based on interfaces. As for the latter two, I am not quite clear yet.

In addition, BLL encapsulates the business logic. In fact, dalfactory is used to generate a dal instance that implements idal, And then it calls the database access layer services, integrates them, and abstracts the service concepts into the service layer concepts. The operation entity comes from the model.

The typical BLL code is as follows:

Public   Class Product {

// Get an instance of the product Dal using the dalfactory
// Making this static will cache the Dal instance after the initial load
Private Static Readonly Iproduct Dal = petshop. dalfactory. dataaccess. createproduct ();

/// <Summary>
/// A Method to retrieve products by category name
/// </Summary>
/// <Param name = "category"> The category name to search </Param>
/// <Returns> A generic list of productinfo </Returns>
Public Ilist <productinfo> getproductsbycategory ( String Category ){

// Return new if the string is empty
If ( String . Isnullorempty (Category ))
Return New List <productinfo> ();

// Run a search against the data store
Return Dal. getproductsbycategory (category );
}

/// <Summary>
/// A Method to search products by keywords
/// </Summary>
/// <Param name = "text"> A list keywords delimited by a space </Param>
/// <Returns> An interface to an arraylist of the search results </Returns>
Public Ilist <productinfo> getproductsbysearch ( String Text ){

// Return new if the string is empty
If ( String . Isnullorempty (text. Trim ()))
Return New List <productinfo> ();

// Split the input text into individual words
String [] KEYWORDS = text. Split ();

// Run a search against the data store
Return Dal. getproductsbysearch (keywords );
}

/// <Summary>
/// Query for a product
/// </Summary>
/// <Param name = "productid"> Product ID </Param>
/// <Returns> Productinfo object for requested product </Returns>
Public Productinfo getproduct ( String Productid ){

// Return empty product if the string is empty
If ( String . Isnullorempty (productid ))
Return New Productinfo ();

// Get the product from the Data Store
Return Dal. getproduct (productid );
}

Public Bool Updateproduct (productinfo)
{
If (Productinfo = Null )
Return False ;
Return Dal. updateproduct (productinfo );
}
}
Copy code

This is the product class in BLL. First, use dataaccess. createproduct (); To call the dalfactory service and generate the corresponding idal instance. Then, call the database access layer service. Provides services at the business logic layer.

Updateproduct is my own method.

In this way, the service in the business logic layer is called at the UI Layer to complete user requests.

OK. Here is the introduction to the database access layer. However, the IOC topic still needs to be studied in depth.

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.