Microsoft PetShop 3.0 Design and Implementation - Data Access Layer

xiaoxiao2021-03-06  70

Microsoft PetShop 3.0 Design and Implementation - Data Access Layer

Recently, the multi-layer design implementation and .net have been interested, thereby studied more famous multi-layer examples - PetShop, the current version is 3.0, and the previous version has a certain difference from design, it should be and Java's PETSHOP design is quite.

About some of Microsoft PetShop, how to install, how to perform business processes, database table structure, etc. Please refer to the article below.

http://msdn.microsoft.com/library/en-us/dnbda/html/bdasamppet.asp

In addition, I recommend watching this article first:

http://msdn.microsoft.com/library/en-us/dnbda/html/petshop3x.asp

(If there is format problem, you can go to http://www.surfsky.com/bbs/myfiles/mspetshop3.0 Report.doc

download)

This article will be analyzed in the way designed and implemented, which is also our greatly practical software developer style. Let's take a look at the form of design and specific implementation of the VS.NET project.

MSPETSHOP 3.0 system structure diagram:

From the figure, you can see that the system is generally divided into three layers of Presentation, Business Logic, Data Access, and sub-layers per layer. Each layer (also included sub-layers), and collaborate each other, this paper is based on this chart, from bottom to date.

Corresponding to the image, the specific .NET Project implementation list (using the list in the MS article without translation)

PROJECT

Purpose

BLL

Home for Business Logic Components

Configtool

Administration Application Used to Encrypt Connection Strings and Create Event Log Source

Dalfactory

Classes use to determine Which Database Access Assembly To Load

IDAL

Set of interfaces which need to be used by Each Dal Implementation

MODEL

Thin Data Classes or Business Entities

Oracles

Oracle Specific Implementation of The Pet Shop Dal Which Uses The iDal Interfaces

Post-build

Project To Run Post Compile Actions Such As Adding Assemlies To The Gac Or Com

Pre-build

Project To Remove Assemblies from the Gac or Unregister AssemBlies from Com

SQLServerdal

Microsoft SQL Server Specific Implement Of The Pet Shop Dal Which Uses The iDal Interfaces

Utility

Set of helper classes inclished a wrapper for the dpapi

Web

WEB PAGES AND CONTROLS

Solution items

Miscellaneous Items Used to build the application soed to sign.snk key file used to sign application assemblies In addition, when I wrote this article, I watched the source code on one side, so I would like to have a PETSHOP3, because time is rushing, the level is limited If I don't give me email correction. Email: cocoboy79@163.com QQ: 364941

First let's take a look at the DAL layer.

One: Data Access Layer:

1 Petshop.utility is shown below: (UTILITY is implemented in the above table)

As described above, this name space has two classes of the CONNECTIONINFO for encrypting the decryption database connection information, and the other DataProtector calls Crypt32.dll and kernel32.dll implement some underlying data security operations, this class is below PETSHOP The .xxxdal namespace call, you can see Petshop.utility just the role of data access assistance tools.

2 petshop.sqlserverdal - System structure diagram DAL layer SQLSERVER DAL sub-layer implementation

The SQLHELPER class actually encapsulates some common functions about database operation access in this system, where it also calls the CONECTIONINFO class method in PETSHOP.UTILITY to encrypt the decryption connection string, such as: ConnectionInfo.DecryptdbConnectionstring method. The SQLHELPER class is based on Microsoft Data Access Application Block for .NET. This thing is used to help users access data in .NET. Such as MS passage:? Are you involved in the design and development of data access code for .NET-based applications Have you ever felt that you write the same data access code again and again Have you wrapped data access code in helper functions that? Let you call A Stored Procedure in One Line? IF SO, The Microsoft® Data Access Application Block for .NET is for you. In fact, you can write a similar SQLHELPER's thing to achieve the generalization of the database's operation to reuse in each item, of course, you can also use the current MS to do this SQLHELPER or Microsoft® Data Access Application Block for. .NET, avoid writing the same duplicate database access procedure in different projects. It is best to look at the specific program of SQLHELPER implementation and the Microsoft Data Access Application Block for .NET mentioned. But here our SQLHELPER should only be partially implemented. For more comprehensive information, please refer to: http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp

The Account class operates to the user account such as INSERT, UPDATE, SIGNIN, where the operation of the database is implemented using the SQLHELPER class above. In addition, Inventory and ORDER, PRODUCT, PROFILE, and Account classes are equally operated on database-related tables, the program style, the operations of the database in these classes are performed by the SQLHELPER class under this name space, for example, the following statement : Private const string sql_insert_signon = "INSERT INTO SIGN VALUES (@UserID, @password)";

Private const string parm_user_id = "@userid";

Private const string parm_password = "@password";

To define a SQL statement, as well as declaring the variable parameters, then perform the appropriate method of the SQLHELPER class below:

SQLHELPER.EXECUTENONQUERY (Trans, CommandType.Text, SQL_INSERT_SIGN, SIGNPARMS);

Finally, in the SQLHELPER.EXECUTENONQUERY implementation, then call the relevant classes in ADO.NET to eventually execute the operation of the database, see SQLHELPER is here again to encapsulate ADO.NET related classes to optimize data operations. Just as SQLHELPER.CS Note Tips: The SQLHELPER CLASS IS INTENDED TO ENCAPSULATE High Performance, Scalable Best Practices for Common Uses of Sqlclient. Below is the implementation of SQLHELPER. EXECUTENONQUERY:

Public Static Int ExecutenonQuery (String ConnString, CommandType CmdType, String CmdText, Params Sqlparameter [] cmdparms) {

// Note: The argument of cmdtext at runtime is SQL_INSERT_SIGNON

SQLCommand cmd = new sqlcommand ();

Using (SqlConnection Conn = New SqlConnection (Conntring)) {

PrepareCommand (CMD, CONN, NULL, CMDTYPE, CMDTEXT, CMDPARMS);

INT VAL = cmd.executenonquery ();

CMD.Parameters.clear ();

Return Val;

}

}

Declarations of Inventory and Order, Product, Profile and Account are like public class account: Iaccount, which implements a related interface like Iaccount is declared in Petshop.IDal, see later.

3 PETSHOP.Oracledal --- System Structure Diagram of OracleDal sublayer

Personally think that the structure should be the above PETSHOP. SQLSERVERDAL, the other SQLHELPER becomes ORAHELPER, of course, in Orahelper, the connection operation of a specific Oracle database is specifically implemented, and the source program is obviously the original SQLCommand cmd = new sqlcommand (); It became oracleCommand cmd = new oracleCommand (). Note: There is also two XXX DAAB sub-layers in the system structure diagram, where is the corresponding implementation? The following correspondence:

The following is the left side is part of the DataAccesslayer in the figure, the right side is the name space or class of the specific implementation.

SQLSERVER DAL - PETSHOP.SQLSERVERDAL namespace

SQL DAAB - PETSHOP.SQLSERVERDAL.SQLHELPER Class

Oracle Dal - Petshop.Oracledal Name Space

Oracle Daab - Petshop.Oracledal.orahelper class

4 PETSHOP.IDAL Data Access Interface - Dal Interface in the Word System Structure Diagram

The interface is a series of declarations or lists of 'function'. The interface does not implement detail. If the interface Iaccount definition can also be seen that Iaccount is only claim:

Using system;

Using petshop.model;

Namespace PETSHOP.IDAL

{

// INTEFACE for the Account Dal

Public Interface Iaccount

{

// Authenticate a User

AccountInfo Signin (String Userid, String Password);

/// Get a user's address stored in the database

AddressInfo getaddress (String UserID);

/// Insert An Account Into the Database

Void INSERT (AccountInfo Account);

/// update an account in the database

Void Update (AccountInfo Account);

}

}

You only need to call the interface, not how the interface is not implemented, call it? In fact, the implementation of the interface is made by a class, then the Iaccount interface here is implemented by the Petshop.sqlServerdal.account class or the Petshop.OracleDal.account class, from their definitions you can see:

Public Class Account: Iaccount {....}

Why is two classes to achieve the same interface is 'or'? Because the purpose of using the interface here is to unify 'appearance', when the upper BLL layer is called, this interface is not known which type of interface is implemented. Who is to determine which type of implementation? Please look down again.

(Other interfaces under Petshop.IDAL are the same as Iaccount, so this is slight.)

5 Petshop.dalFactory Data Access Factory

Factory model is a kind of design model, just like Factory words, I don't need to know how products produced in the factory, you can use things to produce it in the factory. MspetShop3.0 uses factory mode to implement the operation of SQL Server and Oracle database, and users don't need to know which database does not care about the rear desk, it is only necessary to use the interface, the interface is defined. The method to be used, which will call the underlying data access operation according to the specific situation when the interface is called. Now this DalFactory is the key. When the BLL layer wants to manipulate the database, DalFactory will use the SQLServerdal and OracleDal described herein as described in the specific situation. This allows the upper layer to adjust, and the next layer is realized, and only the upper level is applied, and the lower level is worked. For the upper layer, the implementation details are hidden. So how do DalFactory decides to use SQLSERVERDAL to use ORACLEDAL? We will then analyze.

The following is the implementation of the PETSHOP.DALFACTORY.ACCOUNT class:

Namespace petshop.dalfactory {

///

/// Factory ImplementAion for the Account Dal Object

///

Public Class Account

{

Public static petshop.idal.iaccount create () // <<< ----- Here you return to the interface

{

/// Look Up The Dal Implementation We Should Be Using

String path = system.configuration.configurationSettings.AppSettings ["Webdal"];

String classname = PATH ".account";

// using the eviden in the config file loading the appropriate assembly and class

Return (PETSHOP.IDAL.IACCOUNT) Assembly.Load (path) .createInstance (classname);

}

}

}

The following is part of the node in Web.config:

The above CREATE () method returns the IACCount interface, with system.configuration.configurationSettings.appsettings ["Webdal"]; can get the Web.config Node About which data should be used in the system (SQL ServerDal or ORACLALDAL) )Information. Because I choose SQL Server when I install PetShop 3.0, this is: value = "petshop.sqlserdal", if it is Oracle that is value = "petshop.Oracledal"! And this file should also be changeable. Next, ClassName = PATH ". Account" is returned should be PetShop.sqlserdal.account, then use Assembly.Load to load PetShop.sqlserverdal.dll, and create an instance of petshop.sqlserverdal.account and use the interface (Petshop.Idal. Iaccount) Type Back. This will use the PETSHOP.SQLSERVERDAL.ACCOUNT class to implement code. (Back to the 4th, look at it)! This is based on the configuration description of the current web.config file (this should also be the actual configuration of the system ", the BLL layer is just like this:

// Get An Instance of the Account Dal Using The Dalfactory

Iaccount Dal = PETSHOP.DALFACTORY.ACCOUNT.CREATE ();

Accountinfo Account = Dal. Signin (userid, password); // << ß ---- Take a look at the Iaccount interface above 4

You can directly call the interface method to operate the database through the lower DAL layer (in this specifically for user account related operations), and the BLL layer doesn't know how to access the database through SQLSERVERDAL or ORACLDAL, this is determined by Dal Factory, what are you using? Database and underlying details, do not know if BLL knows that the benefits of this is that the BLL layer and the upper program will not or a little rash due to the underlying program changes, because the interface is called in the BL, as long as the interface is defined Change, everything is still OK.

6 Petshop.configtool

First, there is an app.config file in ../microsoft/petshop/configtool/, look at the content, define two database connection strings, there is a line in app.config identifies the relative position of the web.config configuration file used by the ASP.NET program. Then look at the source code of the PETSHOPConnectionstring method, this class first is read from the web.config file from the current directory app.config file, as follows:

Public static readonly string configfile = configurationSettings.appsettings ["WebConfigFileLocation"];

Then statement

XmLDocument Doc = New XmLDocument ();

Doc.Load (configfile);

Load the web.config file, and finally write the encrypted connection string into the XML node corresponding to Web.config. For ASP.NET applications. Where encryption is still using Petshop.Utility.

Configconsole, call PETSHOPConnectionstring class EncryptConnectionstring executes encryption of the coupling string. In addition, the PETSHOPEVENTLOG class is also used in ConfigConsole. Used to record program logs.

Therefore, the connection string of web.config is encrypted at the last deployment. 7 PetShop.Model business entity model

http://www.surfsky.com/bbs/myfiles/7.bmp This originally wants to say when the BLL layer is analyzed, but these models are used in SQLServerdal and OracleDal, no matter how, the upper program performs the final result It is to operate the database, and the database is a relational. It is not object-oriented. It has to abstract the plane 'table' combined with the business rules, so that the way to make the upper layer (BLL and above) as self in operation, not a database Table, so that the 'they' feel that there is no database existence, and the upper layer is only possible to program objects. Similar to the O-R mapping, O-R mapping is complicated than this simple data to the object. It is said that the upper application code can be changed without changes in the table structure, as long as the O-R mapping is changed. The SQL statement of PETSHOP's SQLSERVERDAL and ORACLEDAL is seen in Section 2 above, and then pass the SQL statement to SQLHELPER execution according to the above-mentioned call: Private void setAccountparameters (Sqlparameter [] PARMS, AccountInfo ACC) {

PARMS [0] .value = acc.email;

PARMS [1] .value = acc.address.firstname;

PARMS [2] .value = acc.address.lastname

PARMS [3] .value = acc.address.address1;

PARMS [4] .value = acc.address.address2;

PARMS [5] .value = acc.address.city;

PARMS [6] .value = acc.address.state;

PARMS [7] .value = acc.address.zip;

PARMS [8] .value = Acc.address.country;

PARMS [9] .value = acc.address.Phone;

PARMS [10] .value = acc.userid;

}

PARMS [X] is the parameters in the parametric SQL statement that is declared as a constant, where the field of AccountInfo in Model and these parameters mapping. So for the BLL layer, you only know these models, anyway, finally in SQLServerdal or OracleDal will mapping to the parameter to access the database (why not directly mapping to the field of data sets? I think it should be Because the database operation here is directly for SQLHEPER, it is not necessary to use SQLHELPER. So the XXX DAAB layer in the figure, so mapping is then transmitted to the DAAB to process) Data Access Layer Summary: DAL Completes Database Access Tasks, the upper layer (BLL) is required to call the interface, do not know the specific access details, implemented with the Factory mode, read the web.config method when running, to get the connection configuration information, and finally select one of SQLSERVERDAL or ORACLEDAL. Specific sub-layers while using one of SQLHELPER or ORAHELPERs to complete database operations.

2003-9-19 cocoboy79

转载请注明原文地址:https://www.9cbs.com/read-119474.html

New Post(0)