Analysis of the data access layer technology under .NET

zhaozj2021-02-16  65

http://www.uml.org.cn/net/200441858.htm zhangxuefeng (original) 9CBS

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.

It can be exhausted, in short, there is a little bit of certainty, it is a DataSet choice. Dal in .NET 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 the 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)!

other

End ADO.NET before analyzing, you have to mention the brothers between DataReader and DataSet

Argument.

On the author's information, almost all recommendations are recommended, and the rest is left.

I rarely, I'm decided by personal habits.

When studying ADO.NET, the author is also holding such an idea and remembers the information.

The terms summed up (just like the 23th of the year, it can be almost

After j), I think that I have a big exhibition in ADO.NET on ADO.NET.

It is a pity that the reality is not willing to make a few projects in a row, regardless of the size of the size.

Using a DataSet solution!

At this point, look back to see the most frequent PETSHOP project when learning ADO.NET.

Two comparisons, this is to see a little bit.

Simply put, PETSHOP adopts this "curved country" way to achieve data.

exchange:

DataReader Get Data => Create a Data Entity Class => According to Field Type Fill

According to physical classes => Add a data entity to the list class (only more than one data is returned)

occasion)

(Supplement: Adopt data entity or set class can be more convenient to implement Cache Manament,

And ordinary DataReader cannot meet this needs due to its data reading mode

This process is similar to the effect of DataAdapter.Fill (), but only, in Fill () DataAdPater automatically creates DataReader to get data, then create

DataTable (equivalent to data entity) and populates DataTable according to the field type, of course

If you might return multiple records, DataTable is completely handled, there is no need to implement columns.

The table is operated.

Maybe readers will have questions immediately: In this case, why does PETSHOP still need data

Physique?

There are still some differences.

First, the data entity class is a lightweight structure, which generally contains only the data field, no

What way of operation, this is better than DataTable or DataRow still has some performance advantages.

(It can be ignored when the amount of data is not large; on the other hand, the operation of the data entity is relatively

Simple, do not need developers to have any ADO.NET knowledge (actually DataTable

Said, this is nothing wrong), and some attributes can be.

However, according to the author's practice, these two does seem to be indiscriminate

DataReader program, the reasons are listed below:

(1) For occasions with large data volume, it is possible to use a batch reading method, which is a bit similar to the data paging effect of DataGrid;

(2) For simple data, the physical classes can also be copened that once the associated data is involved, it can only be written separately. And all of this, is very easy to handle in DataSet (for enterprise applications, most of the cases need to handle more complex data);

(3) DATATABLE "Natural" supports data collection operation, such characteristics are more easily controlled than "PETSHOP), which is more natural;

(4) The entity class needs to determine all data types when declaring. When data is filled, DataReader needs to pay another data type corresponding to the entity, and cannot have a slight error! In this regard, DataTable is very convenient and only one type of payment is only necessary;

(5) DataSet solutions can be very convenient to support serialization operations (such as remote, webservices), while relationships with XML are more intimate, which is also critical for interaction with other systems.

It has been analyzed some technologies and programs, and believes that readers have some experience. On the occasion of this receipt, if it is necessary to provide a "sum mention" here, the author's suggestion is very clear:

In enterprise application development, use Dataset (DataTable / DataView) Cache Management solution as much as possible!

In other developments, only the following 4 cases consider using DataReader (in terms of author experience, mostly using DataReader are the second case):

(1) When the resource requirements are more demanding, the resources here are mainly measuring memory and database connections;

(2) It is desirable to perform a custom processing when reading the database returns, for example, terminate the processing immediately after reading a record, or performs a calculation operation when reading. (Tip: This situation is similar to the SAX (Simple API for XML) technology in XML, there is no need to read all XML data at once; the opposite, DOM (DOCUMENT OBJECT MODEL) requires that all XML data must be loaded Can you start the operation (MSXML4.0 has begun to allow only the XML document section to start operation, this is the later words)!)

(3) I only want to get the number of fields returned or returned to the record, such as:

String getNameByid (int NID) // Returns the name of the employee according to the employee ID, only need

// read the name field;

(Tip: This situation can generally be solved directly by performing a specific query or stored procedure)

(4) For some considerations (eg, the responsibility between each of the Layer in the N-Tier system), it is impossible (or prohibiting) to filter it through the database itself, then only use DataReader to read when reading Filter operation!

(Tip: Although DataView can also achieve this, its filtration premise is that all returned data must be read, so it is worse than DataReader!)

u o / r mapping

O / R Mapping's full name is: Object Relational mapping, main purpose is to build a mapping relationship between traditional RDBMS and Oo Language, so that developers completely detach the data for a long-lasting sea.

About O / R mapping or recently comparing O / X mapping (You can refer to "programmer, 2004.01, p86"), may require a special article to discuss, this paper is mainly for the advantages and disadvantages of existing programs. Simple profiles and provide reference information in some practices.

Comparison of J2EE platforms, O / R mapping under .NET can be described as there is no history, and has not yet been tested programs. However, with the attention of major manufacturers, the pace of .Net O / R mapping has begun to keep up with the steps of this item to join the J2EE, and will not make more Many developers put .NET camp? J), which also makes numerous fatigates who come back and forth in SQL CLAUSE or ADO.NET to see "Light Road".

Next, let's take a look at the Solution of this slogan to discuss more than ADO.net.

? Open Source

Open source has always been with .NET keeps a certain distance, O / R mapping is even more than, but in terms of the author's download trial and source analysis, individuals think that the following two solutions still have a certain reference value: OPF, OJB.

Introduction to these two open source projects, you can refer to "Programmer, 2004.01, P13".

The full name of OPF is: Object Persistent Framework.

The full name of OJB is: Object Relational Bridge.

In implementation, the idea of ​​these two programs is completely different, with respective representation. The route of the OPF is a bit similar to Typed DataSet or Borland Eco (please refer to the following description), the implementation is relatively simple, providing more source level control; and the OJB implementation is similar to Microsoft Objectspaces (please refer to the following introduction), use The way the configuration file is complicated, relatively complicated.

The basic framework of these two programs is as follows:

OPF:

It is not difficult to see from the figure:

(1) The Persistent class plays a DataSet role. In addition to the regular object data operations, the PersisTent operation can also set the relationship between different objects (such as the primary relationship, collection relationship, etc.) in the code generated by the Borland ECO. Slightly one or two), this is also the reason why the above say "provide more source level control";

(2) PersistentSqldataManager played the role of DataAdapter, performs real database operations by pre-set Commands; in actually written Employee Data Manager, developers do need to provide basic SQL statements, just like setting in SQLCommond (Borland ECO is further, replacing SQL with OCL);

(3) ObjectBroker's role is very important, it is the bridge between the objects and the data, the RegisterPersistent method establishes this virtual (Object) and reality (RDBMS) relationship;

(4) In the declaration of the Employee Business Object, the correspondence between the object attribute and the database field is reflected in the .NET Attribute mechanism, so modify it is still more convenient, although it is not flexible than the configuration file (please refer to OJB) Introduction), such as: need to recompile, developers have to pay attention to database fields.

OJB:

It is not difficult to see from the figure:

(1) The implementation of the scheme is more complicated, but the user needs to be actually written (only written EMPLOYEE Business Object), the key is to introduce the configuration file; at the same time, we are There is no need to call the RegisterObject method in similar OPF solutions (please refer to the OPF class diagram above), all of which (even database connection information), the system is as good!

(2) In this scenario, the SQL command is thoroughly replaced by the Criteria class, while QueryFacade acts as an Adapter functionality, communicating with the database through the true Command of PersistenceBroker;

(3) Whether it is a repository.xml configuration file, or the Criteria, QueryFacade class, we can find similar implementations in ObjectSpaces (please refer to the following introduction), at the same time, the author believes that this way It is also more in line with the spirit of O / R mapping, reducing the burden of developers! (4) OJB has a very cool tool "repositorygen.exe", can be used to generate the repository.xml configuration file (the same, source code is unpaid J), this, even ObjectSpaces can not do (want Think so many fields, attributes, associations, mapping, simply let people go crazy J)!

Microsoft ObjectSpaces

This is a technique that makes many .NET Guy outstrettime a few years ago. As far as the author said, at that time, as long as I mentioned this topic, I usually returned to J2EE Guy's ridicule, I can't help myself, I'm also eNB (relative EJB) or NCMP (relative CMP).

Finally, we can find it in the "Fang Rong" in the .NET Framework 1.2 (can be found in VS.NET 2004WHIDBEY or YUKON.

First, let's see what the code written by ObjectSpaces looks like (still using the Employee example above):

// Initialize ObjectSpace

SqlConnection conn = new sqlconnection ("Data Source = localhost;

Integrated Security = SSPI; Database = Northwind ");

Objectspace OS = New ObjectSpace ("Map.xml", Conn;

/ / Return to its Title according to EMPLOYEEID

Employee OEMP = (Employee) Os.GetObject

New ObjectQuery (TypeOf (Employee), "ID = 1"));

// Note: The actual field name is: EMPLOYEEID

String startle = OEMP.TITLE;

......

/ / Return to all qualified EMPLOYEEs according to City

ObjectSet oset = Os.GetObjectSet

New ObjectQuery (TypeOf (Employee), "City = '" seattle' ");

// Note: Returns not DataTable, but an object collection

Foreach (Employee Oemp In OSET)

{

... // Note: You can do anything about OEMP here.

}

In response to the second paragraph of the above, there is a solution that replaces ObjectReader instead of ObjectSet, which is similar to the DataSet in ADO.NET 1.0 (ADO.NET containing ObjectspacesD, also known as ADO.NET 2.0). / DataTable is different from DataReader (have to admire Microsoft's exhibited foreigners in front and rear and rear and conformance). Carefully analyze the above code, you can find that it is an amazing similar point with the OJB discussed earlier (OJB Care only draws the basic graph, but it can see this ideological approach)!

For example, the ObjectSpace class provides QueryFacade feature in OJB; the ObjectQuery class provides the criteria feature in OJB; at the same time, the two solutions do not penetrate the same configuration file to store O / R mapping information; It is also very convenient to perform data operation through these 2 classes. A slight difference may be on the data return format (this, ObjectSpaces considerate more details, you can refer to the code above), but this has not imposed on the actual code.

If you make a comparison of the code written under ObjectSpaces in ADO.NET, it is not difficult to see that the code given by ObjectSpaces is more readily read and understood, even if it is not familiar with the development of ADO.NET overall architecture. Personnel, you can easily pick up (only if the code involving RDBMS is only required to establish a database connection). For friends who have been familiar with ADO.NET or have been touched O / R mapping (such as hibernate under J2EE), it can be taken as a small dish!

From the .NET Framework 1.2 document, you can know that ObjectSpaces provides a total of 3 namespaces, and the overall structure is very clear:

System.Data.Objectspaces

System.Data.ObjectSpaces.Query

System.Data.Objectspaces.Schema

Objectspaces, Query has been seen in the code above, can guess from the name, they are responsible for providing basic access interfaces (such as query, increase / deletion / change, etc.) and analyzes various query conditions (such as object filtering, etc. ), SCHEMA namespace is primarily used to operate the O / R mapping profile and serve the class in other two namespaces.

In ObjectSpaces, the O / R mapping profile mainly refers to Map.xml. The name of this file can be replaced at will, which is similar to the repository.xml in OJB. The other two configuration files describing the database structure and object structure are also very important: RSD.XML (Relational Schema Definition), OSD.XML (Object Schema Definition). You can understand the XSD files in Typed DataSet, without them, all data / object mapping and validation will be "illegal" J!

In this article, the author is not prepared to explore ObjectSpaces, nor will it provide any Sample to explain its superiority. In this regard, .NET Framework SDK has long provided rich package.

The author just hope that if it is analyzed from DAL, ObjectSpaces technology can bring us what it means to bid farewell to DataReader / DataSet, or bring new troubles to developers? If you don't have much benefits, you can justify only a few cases:

(1) Objectspaces all use object-like access data, greatly eased the SQL (or RDBMS) phobia of many developers;

(2) For the relatively simple database structure, you only need to modify the configuration file, there is no need to recompile the code (compared to the mapping relationship in the OPF, it is more flexible and convenient);

(3) For more complex database structure changes, the modified work is more simple than before because of the object operation.

(4) After using the O / R mapping profile, database design and DAL development can be performed separately, and the impact of mutual effects has also dropped;

Insufficient is what we are more concerned about:

(1) The current version does not support Chinese (eternal topic J) query, not cool!

(2) The current version only supports the database system, weak (this is a very intriguing restriction, interested readers may wish to think about what is the reason)!

(1, 2), these two points have excluded a lot of friends who are eager to try. And the author's participation .NET project is not affected by 1, but since Oracle often uses Oracle, you have to take love. J)

(3) Performance issues. Although ObjectSpaces also provides a DataRectReader, but after all, it is necessary to make a data power type fill, no matter how losses will be lost, if the amount of data returns to the data, it will be a question that must be considered;

(4) Or performance issues. Map.xml is a good stuff, but how to optimize its access and correct validation (RSD.xml, osd.xml) take time, even at some time (database structure is more complicated), this will result Three more serious consequences;

Saying some shortcomings, in fact, there is no need to worry too much. After all, there is no perfect solution, how to pay attention to your own decision.

In this chapter, the author gives a summary for you to refer to one. After all the analysis, the author also tried to provide "My Program (Writing)" in conjunction with your own practice, I hope to help readers.

About the Author:

"Zhang Xuefeng, this article, is the senior development engineer of Bibo Global Development Center. He currently works in the Core / Eai department of China Shanghai Bibo Global Development Center, engaged in .NET technology research and related projects. You can pass xuefeng.zhang@bearingpoint .com Contact him. "

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

New Post(0)