Microsoft PetShop 3.0 Design and Implementation - Data Access Layer

xiaoxiao2021-03-06  76

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. For some information about some Microsoft Petshop, how to install, the performance business process, the database table structure, etc. Basic information, please refer to the article below http://msdn.microsoft.com/library/en-us/dnbda/html/ BDASAMPPET.ASP additional suggestion first look at this article: http://msdn.microsoft.com/library/en-us/dnbda/html/petshop3x.asp (If there is format issues) to http: //www.surfsky This article will be analyzed in the way designed and implemented in the form of software developers in design and implementation, this paper will be analyzed in the way designed and implemented. Let's take a look at the form of design and specific implementation of the VS.NET project. MSPETSHOP 3.0 System Structure Map: From the figure, you can see the system is generally divided into three layers of Presentation, Business Logic, Data Access, and sub-layers in each layer. Each layer (also included sub-layers), and collaborate each other, this paper is based on this chart, from bottom to date.

On the map, a list of specific .NET Project realization (list of borrowed MS article without translation of it) Project PurposeBLL Home for business logic componentsConfigTool Administration application used to encrypt connection strings and create event log sourceDALFactory Classes used to determine which database access assembly to loadIDAL Set of interfaces which need to be implemented by each DAL implementationModel Thin data classes or business entitiesOracleDAL Oracle specific implementation of the Pet Shop DAL which uses the IDAL interfacesPost-Build Project to run post compile actions such as adding assemblies to the GAC or COM Pre-Build Project to remove assemblies from the GAC or unregister assemblies from COM SQLServerDAL Microsoft SQL Server specific implementation of the Pet Shop DAL which uses the IDAL interfacesUtility Set of helper classes including a wrapper for the DPAPIWeb Web pages and controlsSolution items Miscellaneous items Used to build the application such shop.snk key file used to sign application assembly, I am writing this article is one Write the source code on one side, so I would like to have a PETSHOP3, because the time is rush, the level is limited, if I don't give me email correction. Email: cocoboy79@163.com QQ: 364941 Let's take a look at the DAL layer. One: 1: 1 PETSHOP.UTILITY as shown below: (UTILITY UTILITY) is as described above, this name space has two classes to encrypt the decrypted database connection information, another DataProtector Call CRYPT32.DLL and KERNEL32.DLL implement some underlying data security operations, this class is called in the PETSHOP.XXXDAL name space, and it is visible to PetShop.utility just to play the role of data access assistance tools. 2 PETSHOP.SQLSERVERDAL - System Structure Diagram The SQLServer DAL sublayer in the DAL layer implementation SQLHELPER class is actually packaged with some common functions about database operations accesses in this system, where it also calls the above PETSHOP.UTILITY CONECTIONINFO class method Encrypts 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 a generalized operation of the database to reuse in each item, of course, can you 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 Account class to operate the user account such as Insert, Update, Signin, where the database Operation, 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 SignOn VALUES (@UserId, @Password)"; private const string PARM_USER_ID = "@UserId"; private const string PARM_PASSWORD = "@Password"; to define a sql statement, and the statement in which Change parameters, then execute: SQLHELPER.EXECUTENONQUERY (Trans, CommandType.Text, SQL_INSERT_SIGN, SIGNPARMS); Finally, in the SQLHELPER.EXECUTENONQUERY implementation, call the relevant class in ADO.NET For the database, it can be seen that SQLHELPER has an ADO.NET related class here to optimize data operations.

As SqlHelper.cs in Notes Tip: The SqlHelper class is intended to encapsulate high performance, scalable best practices for common uses of SqlClient Here is SqlHelper ExecuteNonQuery implementing content:.. Public static int ExecuteNonQuery (string connString, CommandType cmdType, string cmdText, params SqlParameter [] cmdParms) {// NOTE: cmdText runtime argument is SQL_INSERT_SIGNON SqlCommand cmd = new SqlCommand (); using (SqlConnection conn = new SqlConnection (connString)) {PrepareCommand (cmd, conn, null, cmdType, cmdText , cmdparms); int val = cmd.executenonQuery (); cmd.Parameters.clear (); return val;}} Declaration of additional inventory and order, product, profile, and account classes are like public class account: Iaccount A associated interface, an interface like Iaccount is declared in PETSHOP.IDAL, see later. 3 PETSHOP.ORACLEDAL --- System Structure Diagram OracleDal sublayer Realizes individual thinking structure should be the above PETSHOP. SQLSERVERDAL, and SQLHELPER becomes orahelper, and in Orahelper, it is certainly specific to a specific Oracle database. Operation, look at the source program is obviously the original SQLCommand cmd = new sqlcommand (); becomes 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 is: The following is the left side is part of the DataAccesslayer in the figure, the right side is the specific implementation of the name space or class SQLServer Dal - Petshop.sqlserverdal name space SQL daab - Petshop.sqlserdaldal.sqlhelper class Oracle Dal - Petshop.Oracledal Name Space Oracle DAAB - PETSHOP.ORACLEDAL.ORAHELPER Class 4 PETSHOP.IDAL Data Access Interface - The Dal Interface Interface is a series of "function" declaration or list in the corresponding system structure diagram, the interface does not implement detail, as the interface IACCOUNT definition statement can be seen only IAccount: 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 Just call the interface without having to implement how the interface is not implemented, what is the use of 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 a Petshop.OracleDal.account class, from their definition: public class Account : Iaccount {......} Why is two classes to implement 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, so this is slight.) 5 PETSHOP.DALFACTORY Data Access Factory Factory Model is a kind of design mode, just like Factory words like Factory, for users, You don't have to know how products produced in the factory, you can use things that produced 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 classes are implemented PetShop.DALFactory.Account: namespace PetShop.DALFactory {///

/// Factory implementaion for the Account DAL object /// public class Account {public static PetShop.IDAL.IAccount Create () // <<< Node in Config: The create () method above, returns the IACCOUNT interface, with system.configuration.configurationSettings.appsettings [" Webdal "]; you can get the system about the system in Web.config nodes Which data access layer (SQLSERVERDAL is still ORACLEDAL) should be used. 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 system's 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 the 4th point to directly call the interface method to operate the database through the lower DAL layer ( In this specific user account-related operation), the BLL layer does not have to know if you should access the database via SQLSERVERDAL, this is determined by Dal Factory, what database and underlying detail, do not use BLL to know, doing this benefits It is the BLL layer and the upper layer of programs that will not be affected by the underlying program changes because of the underlying program, because the interface is called, as long as the interface is defined, 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 of EncryptConnectionString PetShopConnectionString method, first in this class file reading position web.config app.config file from the current directory, as follows: public static readonly string CONFIGFILE = ConfigurationSettings.AppSettings [ "WebConfigFileLocation"]; and Statement XMLDocument Doc = New XMLDocument (); Doc.Load (configfile); Load web.config file, and finally write the encrypted connection string in 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 analyzing the BLL layer, but use these models in SQLServerdal and OracleDal, no matter what, The upper program performs the final result 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) Self-operation, not a database table, so that the 'they' feel that there is no database existence, and the upper layer is Object-oriented. 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.

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

New Post(0)