l
Since .NET truly enters the development person, "efficiency" two words have always become the topic of many programmers. Whether it is from the development model (Cross Language), the system framework (.NET Framework), it is not a victorious person who has a variety of easy tools (VS.NET).
At the same time, on the other hand, whether .NET can truly compete for the heavy responsibility of enterprise application (Enterprise Application), but still arguing, it is very different.
Generally speaking, there are many aspects, such as security, performance, scalability, ease of use. In this article, the author is more willing to discuss the relevant technologies of the data access layer under the .NET, which may be a wide focus on the sensitive topic of the N-Tier Architecture, and for most For developers, this may also be the most depressed part of the project, or even the most controversial part.
In the following discussion, in order to unify, the author temporarily refers to the data access layer is referred to as DAL (Data Access Layer).
l Analysis problem
After simple statistical analysis, it is not difficult to find that DAL is afraid, not for the problem of technical itself, even in contrast, many developers believe that this is one of the most technical parts (on the author's experience) In view, the development time of this layer is generally short, and many developers are unwilling to bear "hardships"), but the architecture needs or certain thoughts (such as: Dal and DAL) have only joined this so-called first Four layers (traditional three-layer architecture did not propose DAL ideas).
DAL's proposal, it is indeed a huge challenge to the traditional architectural model. The purpose of joining is also hoping to borrow him further increase productivity. In this mode, ideal is: Most developers get rid of DBA suffering, or thoroughly Direct relationship with the database, SQL pain will go away from us, the entire OO world is quiet.
However, ideal is ideally, it is necessary to pass the project inspection.
Next, the author attempts to analyze a relatively popular and representative solution to see if there are some valuable conclusions, and provide some reference for us to design and implement DAL in the future.
u ado.net
First, mention the DAL under .NET, immediately reflecting the eye blind is ADO.NET.
That's right, almost all DAL solutions (please allow the author to use Solution rather than
Framework must be developed from it, no choice, this is also .NET features
Implementation (Comparison J2EE).
Exclude business factors and CLR itself, ADO.NET truly brought things to us.
Not much, it is worth mentioning, DataSet (for the author of the author, using more
DataSable and DataView). From Microsoft's early memory database (Memory Database)
Some people ask for today's DataSet, this is a tribute to the words.
Can deal, in short, there is a bit of certain, it is the choice of DataSet, .NET
Dal can be like today, and everyone's ideas can be more open.
Duwamish
There are a lot of good SAMPLE this aspect, the most classic universal development package for Microsoft's strong recommendation: duwamish. For friends who want to learn .NET DAL design, this is a good starting point, this
Common profile, you can refer to "9CBS Development Master, 2003.11", this article is not
Take it again.
The author used the Duwamish program in one project of its own participation, which is limited to work
The period, I feel that this is a good reference, I didn't do in-depth analysis. Recall now
Get up, there is still a lot of shortcomings.
For a simple example, Cache Management is not considered in the DUWAMISH scheme.
And this is an enterprise application, some of the questions that have to be considered;
Aspect, although DUWAMISH say goodbye to the SQL statement (all the stored procedures are implemented),
But database traces are still very obvious, such as: definition of certain field names, association table name
Definition, etc.
There is also a very headache problem that is reflected in the development process. First, then
Some simple data sheets are relatively easy to implement, and some data sheets containing each other
When our Dal engineers felt pressure, they later, almost done DBA.
Work earlier in the modeling of the database, only, this time the database script is changed.
C # implementation (or said: replacing the database structure with an OO characteristic DataSet)
Yes.
It may, DUWAMISH's implementation is more classic, but in practical applications, sometimes
It tastes BEST PractICE. Take our project, although successful delivery, no matter from the mold
The type of multiplexed angle, or the development efficiency, it is very successful. Set of popular words:
In fact, we can do better!
PETSHOP
Another DAL implementation worth a reference on ADO.NET is a famous PetShop.
Of course, similar to duwamish, the famous arrogance is not necessarily practical. Although PetShop is
Make up the shortcomings of duwamish in some respects, for example: support multiple numbers through Factory
According to the reservoir, the Cache mechanism is introduced, providing more convenient SQL Helper, but also
Other questions have been brought. Among them, the most troublesome is the introduction of SQL statements, but also
It is a different SQL statement for different data deposits (mainly SQL Server and Oracle)
The parameter representation is different.
On the other hand, PETSHOP does not use DataSet to be more concise
Entity object (Model), but it still converts the result of DataReader to the included entity pair
The list of pictures of the icon is deployed in line. From this sense, it can be said that the soup is not changed. even,
In some occasions, for example: data is required, or navigate between the master from the data, but
More inconvenient (at this time, simple collection or list is unable to meet demand, DBA
With DAL developers can only provide other methods to achieve purposes).
From the above two examples, we can see that even in Microsoft's development team,
There is no consistency on the issue of DAL. More details on this area, interested
Friends can refer to the following article:
Http://www.microsoft.com/china/community/column/67.mspx. Actual combat
The two solutions are analyzed, let us see their respective advantages and insufficient, and the complex environment of enterprise-class applications is unlikely to ask a framework that can solve all problems, so they can only According to the specific situation, the specific analysis is specifically analyzed.
The author has participated in the development of a (.NET) large outsourcing project, fortunately, his DAL is designed, deeply shocked, here to discuss together with friends.
Take the Northwind database with SQL Server as an example, as follows, a para-based call code (the author has adjusted on some names):
/ / Return to its Title according to EMPLOYEEID
Boemp = new Employeedal ();
Boemp.keys ["EMP_ID"] = 1; // Note: The actual field name is: EMPLOYEEID
Boemp.select ();
String startle = Boemp ["EMP_TITLE"]; // Note: The actual field name is: Title
......
/ / Return to all qualified EMPLOYEEs according to City
Boemp = new Employeedal ();
Boemp.keys ["EMP_CITY"] = "seattle";
Boemp.select (); // Note: This method is identical to the above call
Datarable DTEMP = Boemp.Table;
If you don't consider the object creation (can use Object Pooling or Cached Object), the actual code is only two lines!
More surprisingly, the above Employeedal class does not have any true realization code, just declare the class name, then inherited from a general base class! !
The most elegant place is not here. In fact, even in that base class, you can't see the gang dispute between SqlConnection or Oracleadapter.
I believe everyone also guess, it's right, it is borrowed from the implementation of PETSHOP, using the Factory mode to ensure that DAL can apply to different data sets. However, this implementation is still very different from PETSHOP: At least, it does not produce different SQL statements, and there is no different parameter call mode (SQL Server usually uses "@" symbols, Oracle general use ":": " Symbols), all gangstere the colleagues!
Among them, it is of course benefiting from the implementation skills of Factory, but more important factors are still exempt from design. In fact, in the .NET Framework, this design of the cornerstone is provided, which is white, which is the interface in System.Data (such as IDBConnection, iDataAdapter, etc.).
Based on such a design, we will no longer need to provide different data access to different data deposits for each DAL class. For example: In PETSHOP, the order data needs to implement the Order class, which is natural, the system implements the Order class for SQL Server and Oracle, respectively, and operates with the methods provided by Different Provider (SqlClient, OracleClient). When actual calls, PetShop dynamically create a real ORDER class through the Factory mode and activate the corresponding method, a scheme for different data deposits. In fact, PETSHOP has been more flexible. If you can save the "writing of different Order classes", you really send to the Buddha to get J. And all of these features, in this project participated in the project, it has been fully available!
As for "Employeedal (of course, including all of all DAL classes), there is no real implementation code. VIEW) Store in the XML file according to NameSpace.
You may have seen it, in theory, even only one DAL class can complete all the above work! However, in practice, different DAL classes may still have some subtle differences on data processing (such as data check, format conversion, etc.).
In general, in such a large project, it is impossible to ask all developers (except DBA, Dal Framework Developer to learn about the all aspect of ADO.NET, although the author has research, but in this project, Only two classes from the end of the end: DataTable, DataView (even if transaction does not need to know)!
About the Author:
This article author Zhang Xuefeng is a consultant for Microsoft (China) Co., Ltd. He is currently working in China's Microsoft Advisor Consulting Department, engaged in .NET technology research and development of related projects. You can contact him through xfzhang@microsoft.com.