Analysis of. Net three-layer Classic Architecture petshop3.0-1

Source: Internet
Author: User

Petshop is the PetStore implemented by C #. We will not discuss the specific technical issues.
Petshop3.0 is significantly different from petshop1 and 2, mainly in design. Let's take a look at the eight projects and one site, and we will know that it must have been divided into many layers.
I. Overview.
Model:
Model layer, which encapsulates business entities and generally corresponds to the database mode.
For example:
Public class accountinfo {

// Internal member variables
Private string _ userid;
Private string _ password;
Private string _ email;
Private addressinfo _ address;
Private string _ language;
Private string _ category;
Private bool _ showfavorites;
Private bool _ showbanners;
...
}
Idal:
The data access interface layer mainly refers to some Dao interfaces.
For example:
Public interface iaccount
{
Accountinfo signin (string userid, string password );
Addressinfo getaddress (string userid );
Void insert (accountinfo account );
Void Update (accountinfo account );
}

Oracledal:
The data access layer implemented by Oracle.

Sqlserverdal:
Data access layer implemented by SQL.
Classes in oracledal and sqlserverdal both implement interfaces in idal. Is a DAO implementation.

Dalfactory
Determines whether to use Oracle or MSSQL. Determine which dal is used through the configuration in Web. config (through reflection, dynamically generate the classes in the workflow class petshop. sqlserverdal or petshop. oracledal namespace ).
<Add key = "webdal" value = "petshop. sqlserverdal"/>
<Add key = "ordersdal" value = "petshop. sqlserverdal"/>
Public class account
{
Public static petshop. idal. iaccount create ()
{
// Look up the Dal implementation we shoshould be using
String Path = system. configuration. configurationsettings. receivettings ["webdal"];
String classname = path + ". Account ";

// Using the evispongiven In The Config File Load the appropriate assembly and class
Return (petshop. idal. iaccount) Assembly. Load (PATH). createinstance (classname );
}
}

Bll:
Business access layer. Read the configuration through dalfactory and decide the Dal implementation to use.
Public class account {
Public accountinfo signin (string userid, string password ){


If (userid. Trim () = string. Empty) | (password. Trim () = string. Empty ))
Return NULL;

// Use dalfactory to call the specific Dal implementation.
Iaccount Dal = petshop. dalfactory. Account. Create ();

// Try to sign in with the given credentials
Accountinfo account = Dal. signin (userid, password );

// Return the account
Return account;
}
...
}

Web:
The presentation layer mainly includes the web page (aspx), user control (ascx) controls, custom server controls simplepager and viewstatepager.

Utility:
Public modules, a group of helper classes, some common methods that may be used by other business and data access layers.




How to make the system more flexible by layering
By studying the object-oriented design and design patterns and architecture patterns, we can understand that the important purpose of design is to be reusable, maintenance, and scalability.
Object-oriented technology itself provides many reusable mechanisms, which can be implemented through inheritance and combination. Code To a large extent. In addition, we need to abstract basic concepts through design and reuse them. Reuse can improve the maintainability of the system, because the reuse of things has generally been fully tested and tested for a long time, which is easier to guarantee than the quality of the wheel we repeatedly invented.
Scalability, inheritance, and overloading are extensions, but they are language-level and we need to achieve system-level scalability through design.
Here we will discuss the design indicators based on the hierarchical design and implementation of petshop.
The call relationships between different layers are as follows:
Web layer --> BLL (use model) --> dalfactory (via idal) --> specific Dal Implementation Layer (Oracle or ms SQL) --> persistent storage.
In this way, as for the adjacent layer interaction at each layer, through well-defined interfaces, the implementation within the layer is not very relevant to other layers. This is in line with the idea of software engineering, because after the interface is defined, developers can implement their own layers according to their own expertise, and it is easy to test, therefore, the efficiency and quality of software development can be improved.
From the architecture and source code implementation, we can see that the flexibility, scalability, and maintainability of the data layer are achieved through the dalfactory layer. We know that because of the interface-oriented programming principle, dalfactory can determine which idal is used through the configuration file information, in this way, you can modify the configuration file to meet your database requirements during deployment. Of course, this also requires that the specific database Implementation Layer should follow specific naming conventions, such as for account implementation, the corresponding classes in SQL and Oracle must have the same name and constructor (now there must be a default constructor, or initialization fails). Of course, the namespace must be different.
Later, when we need to implement the Data Implementation Layer of MySQL or DB2, we only need to implement it through MySQL or DB2 according to the above naming conventions. configuration in the config file (scalability and maintainability ). In addition, if the customer used MSSQL before using Oracle, then ...... We won't be scratching our head if we do so repeatedly, because our original implementation is reusable, new requirements are scalable. On the other hand, we can also adopt different implementation methods for the same database, such as ado.net (also divided into direct SQL and Stored Procedure Implementation), Orm framework, and ibati semi-ORM framework, we can create a project for each of these implementations and make flexible choices, instead of modifying them based on the original ones. This is why the petshop data access layer shows us why it is so layered.
In fact, many frameworks and systems are implemented through configuration to achieve flexibility, because Configuration Modification
Unlike Modification Source code In this case, you need to re-release the data, and the two DLL files, petshop, oracledal, and sqlserverdal, can exist at the same time. You can configure the one you need.

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.