Analysis of data access layer technology under. Net (Iv.)

Source: Internet
Author: User
Tags definition relative
Access | data Ømicrosoft objectspaces

This is a technology that has made many. NET guy stretch his neck a few years ago. As far as the author is concerned, at that time, as soon as the topic is mentioned, it is generally in the laughter of the Java-ee guy, and hates not to get a ENB (relative EJB) or ncmp (relative to CMP) or something.

Finally, we can get a glimpse of the "" in the. NET Framework 1.2 (available in the 2004Whidbey or Yukon, now beta versions).

First, let's look at what code is written with ObjectSpaces (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 its title according to EmployeeID

Employee Oemp = (employee) OS. GetObject (

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

Note: The actual field name is: EmployeeID

string strtitle = Oemp.title;


Return all eligible Employee according to City

Objectset oset = os. Getobjectset (

New ObjectQuery (typeof (Employee), "city =" "Seattle"));

Note: The return is not a DataTable, but a collection of objects

foreach (Employee oemp in Oset)


...//NOTE: You can do anything with oemp here.

For the second piece of code above, there is also a solution to replace objectset with ObjectReader, which contains differences similar to 1.0 (, which contains objectspacesd, also known as 2.0 the difference between the dataset/datatable and the DataReader (I have to admire Microsoft's wily J in consistency).

With a careful analysis of the above code, you can see that it has a striking similarity to the OJB discussed above (OJB the author only draws a basic class diagram, but can see this kind of intellectual proximity)!

For example, the ObjectSpace class basically provides the Queryfacade functionality in OJB, and the ObjectQuery class basically provides the criteria function in OJB, and both solutions use a configuration file to store O/R in the same way. mapping information, and applications typically use these 2 classes for data manipulation, which is very handy. Slightly different may be in the Data return format (this point, objectspaces considered more detailed, you can refer to the above code), but this has little impact on the actual implementation of the code.

If you compare the calling code under ObjectSpaces with the code written in below, it's easy to see that ObjectSpaces's code is easier to read and understand, and that even developers who are unfamiliar with's overall architecture can easily get started (the only code that involves an RDBMS is Required when establishing a database connection. For those who are already familiar with or have contacted O/R Mapping (such as: The hibernate under the Java EE), it is a piece of cake!

As you can see from the. NET Framework 1.2 documentation, ObjectSpaces provides a total of 3 namespaces, and the overall structure is clear:




ObjectSpaces, query has seen in the above code, from the name can be guessed, they are mainly responsible for providing basic access to the interface (such as query, add/delete/change, etc.) and the resolution of various query conditions (such as object filtering, etc.), the schema namespace is mainly used to manipulate O/R Mapping the configuration file and provides services for classes in the other two namespaces.

In ObjectSpaces, the O/R mapping configuration file mainly refers to the Map.xml, the name of the file can be changed at will, compared to similar OJB repository.xml. The other two configuration files that describe the structure of the database and the structure of the object are also important: rsd.xml (relational schema definition), Osd.xml (object Schema definition). They can be understood as XSD files in the typed DataSet, without which all data/objects mapping and validation will be "illegal" J!

In this article, the author is not prepared to a deep exploration of objectspaces, and will not provide any sample description of its advantages, in this regard, the. NET Framework SDK has already provided a rich package.

The author just hopes that if we analyze from the Dal point of view, what objectspaces technology can bring to us, whether it means to say goodbye to Datareader/dataset, or to create new problems for developers?

The benefits are not much said, just to cite a few examples can be clear:

(1) ObjectSpaces all use object way to access data, greatly alleviate the SQL (or RDBMS) phobia of many developers;

(2) for relatively simple changes in the structure of the database, only need to modify the configuration file, without recompiling the code (compared to OPF mapping relationship in the. NET attribute to the code, it appears more flexible and convenient);

(3) For more complex database structure changes, because only involves object operation, so the work of modification is much simpler than before;

(4) After using the O/R mapping configuration file, the database design and the DAL development can be carried out separately, and the influence of each other is reduced to the lowest point;

Shortage is the topic that we should pay more attention to:

(1) The current version does not support Chinese (Forever topic J) Query, unhappy!

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

(1, 2 from the. NET Framework SDK Document, this two point has eliminated a lot of eager friends.) And the author is involved. NET project is not affected by 1, but because of the frequent use of Oracle, you have to temporarily reluctantly J)

(3) Performance problems. Although ObjectSpaces also provides similar DataReader function (ObjectReader), but after all, need to do a strong type of data filling, in any case, there will be losses, if the return data is large, will be a problem to be considered;

(4) or performance problems. Map.xml is a good thing, but how to optimize access to it and make the right validation (based on Rsd.xml, Osd.xml) takes time after all, even at some point (the database structure is more complex), which can result in more serious consequences than the 3rd;

Said some deficiencies, in fact, there is no need to worry too much, after all, there is no perfect solution, how to choose depends on your own decision.

At the end of this chapter, the author gives a summary of his own, which can be used for your reference one or two. After all the analysis has been completed, the author also attempts to combine his own practice to provide "my program (writing)", I hope to give you the help of readers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.