Analysis of the data access layer technology under the .NET (4)

zhaozj2021-02-16  57

Ø 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-24110.html

New Post(0)