In-depth shop PETSHOP-SQLSERVERDAL Data Access to Sample-Duwamish

xiaoxiao2021-03-06  46

In the past few days, I saw the PetShop 3.0 released by DOTNET, compared the original Duwamish data access layer, write some essays ^^

First look at the root of Dalfactory Data Factory and Database in PETSHOP - SQLServerdal, using SQLHELPER.CS file, I believe that everyone will not feel strange to this. Before doing PETSHOP database access, the relationship first Object modeling, that is, the O / R model we usually said, modeled in the [Account] table in the [Account] table in the Modle layer, [Of course, this is the division of the Microsoft Development Team for the granularity of the object You can also extend Balance, etc. to extract the Account section, etc.], this idea can be reduced to the least. And I Like it ^^

Namespace petshop.model {

///

/// Business entity used to model addresses /// [serializable] public class addressinfo {

// Internal member variables private string _firstName; private string _lastName; private string _address1; private string _address2; private string _city; private string _state; private string _zip; private string _country; private string _phone;

public AddressInfo (string firstName, string lastName, string address1, string address2, string city, string state, string zip, string country, string phone) {this._firstName = firstName; this._lastName = lastName; this._address1 = address1; this ._address2 = address2; this._state = state; this._zip = zip; this._country = country; }_phone = phone;

}

The above, I omitted all attributes, only draws the backward model to discuss

At the bottom Idal Interface Data Access Logic Layer We can see the use of SQLCommand.Text and use DataReader to return the result with a similar pointer, only the minimum amount of data required is satisfied, not the entire DataSet as a data transmission medium. ,

The originally do model is specified, so that if the database extension field will be adjusted in the future, you only need to adjust the model and access the function. Other BizLogic is running normally. Take a look at the code:

Namespace petshop.sqlserverdal {///

/// summary description for accountdalc. /// public class account: Iaccount {

private const string SQL_SELECT_ACCOUNT = "SELECT Account.Email, Account.FirstName, Account.LastName, Account.Addr1, Account.Addr2, Account.City, Account.State, Account.Zip, Account.Country, Account.Phone, Profile.LangPref , Profile.FavCategory, Profile.MyListOpt, Profile.BannerOpt FROM Account INNER JOIN Profile ON Account.UserId = Profile.UserId INNER JOIN SignOn ON Account.UserId = SignOn.UserName WHERE SignOn.UserName = @UserId AND SignOn.Password = @Password "; ///

/// pair return result set mapping to address model /// /// /// public AddressInfo GetAddress (string userId) {AddressInfo address = null; SqlParameter [] addressParms = GetAddressParameters (); addressParms [0] .Value = userId; using (SqlDataReader rdr = SQLHelper.ExecuteReader (SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_ADDRESS , AddressParms) {if (RdR.Read ()) {address = new addressinfo (rdr.getstring (0), Rdr.getstring (1), Rdr.ge Tstring (2), Rdr.getstring (3), Rdr.getstring (4), Rdr.getstring (5), Rdr.getstring (6), Rdr.getstring (7), Rdr.getstring (8));}}

Return Address;}

I omitted those fields of connection. In ADO.NET, we all know that DataSet is disconnected, is placed in system memory, PETSHOP is not immediately read after opening the data connection, but passes DataReader A additional object is performed to perform the operation of the data before closing the connection. In this way, the data connection is longer, and the database connection is a very valuable server resource. In contrast, Dawam is filling immediately after connecting the database, and then quickly releases the database connection, which is more conducive to a large number of users. Access. So I think is that the configuration table of MASTER should still read at one time. After all, in general applications, the customer Account table generally does not exceed 10,000, as data high, only the amount of memory is required, then the data is only available. You need to retrieve the RowFilter, avoid the frequent interaction of the link, there will be better results in practical applications. For order orders or the database for realtime access is appropriate. After all, for enterprise development, The robustness, stability, and performance of the program are primarily considered.

Let's take a look at duwamish's data access. The previous work is to correspond to the table DataTable to avoid the next repeated write that many other field names. I am annoyed, that is, the physical activity, I used 110 fields for last maintenance. The architecture is really horror, write a module, I have been taking out 110 fields, TMDPUBLIC CUSTOMERS: IDisposable {/// DataSetCommand Object // Private SqldataAdapter DScommand; //// Stored Procedure Parameter Names // Private Const String PKID_PARM = "@PKId"; private const String EMAIL_PARM = "@Email"; private const String NAME_PARM = "@Name"; private const String ADDRESS_PARM = "@Address"; private const String COUNTRY_PARM = "@Country"; private const String password_parm = "@password"; private const string phone_parm = "@phonenumber"; private const string fax_parm = "@fax";

/// ///// Initialize the internal datasetcommand object. /// public customers () {/// CREATE The DatasetCommand // DScommand = new sqldataadapter (); dscommand.tablemappings.add ("table", customerdata.customers_table);

} // class customer duwamish7.dataAccess

public CustomerData GetCustomerByEmail (String emailAddress, String password) {// // Check preconditions // ApplicationAssert.CheckCondition (emailAddress = String.Empty, "Email address is required", ApplicationAssert.LineNumber!);! ApplicationAssert.CheckCondition (password = String .Empty, "Password is required", ApplicationAssert.LineNumber); // // Get the customer dataSet // CustomerData dataSet; using (DataAccess.Customers customersDataAccess = new DataAccess.Customers ()) {dataSet = customersDataAccess.LoadCustomerByEmail (emailAddress) ;} // // Verify the customer's password // DataRowCollection rows = dataSet.Tables [CustomerData.CUSTOMERS_TABLE] .Rows; if ((rows.Count == 1) && rows [0] [CustomerData.PASSWORD_FIELD] .Equals (password )) {Return dataset;}}}}} Now has now developed a lot of O / R mapping tools, many mapping code can be handed over to them, after all, the business-level development is good, selling some still is your Business logic is strong, can do to analyze problems to solve problems, and provide reference sources in the knowledge base. There is also a benefit with Dataset that is to return Datas ET or DATATABLE, you can use the DataSet.Readxml of .NET Framework, DataSet.Getxml, XML conversion operation, but it is said that the overhead is relatively large. For XML this cross-platform medium, it is basically mainly used for orders, financial statements and There are many configuration sections, and it will not cause great overhead in general applications.

By the way, I still have a rookie-level problem. How can I stick the colorful source, this blogger comes with the code released by the code, it is too ugly, it is not clear, I hope there is Heroes info.thanks ^^

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

New Post(0)