Reconstruction of IBuySpy data access layer with ObjectSpaces

Source: Internet
Author: User
Tags definition bind current time datetime final
object| Access | Data objectspaces This ORM framework may have been heard N times before, it was born very early, but the development cycle dragged on for a long time, although its beta version was already used, but only to the. Net Framework 1.2 plan to formally include it, and put it in the namespace of Object.Data.ObjectSpaces.



ObjectSpaces's circulation version is also very much, has been studied by many people an EAP (Early adopter Preview version number is 1.0.1081, I saw a 1.0.3328.4 in the gotdotnet.com of the version of the thing, here with this objectspaces is now widely used. Net Framework 1.2 The Alpha beta contains the version number consistent with the. Net framework, 1.2.30703.27.



From the change of version number, we can see that the objectspaces has changed a lot from the beginning to the present. The three versions mentioned above are all different, and the differences are not small. Now the latest version I think should not be very different from the final version. My previous article was based on the EAP version, much simpler than the present one.



Getting to the point: in the IBuySpy architecture, there is virtually no BLL (commercial logic layer), and even no entity Class is created, and data is obtained from the database through the DAL (data Access layer). The dataset or DataReader is then passed directly from the DAL to the ascx file that forms the module of the IBuySpy page, and is directly bound to Web control such as DataList.



We re IBuySpy The DAL of the announcement module in ObjectSpaces:



First, take a look at IBuySpy the original announcements module



DAL:AnnouncementsDB.cs file

Interface Controls page: Announcements.ascx controls for display and editannouncements.aspx pages for new modifications



Inside are the most standard writing, nothing to say.



Second, Entity Class



Creating a new announcement class and a Announcementcollection collection class is nothing to talk about.



Third, the establishment of ObjectSpaces RSD, OSD, MSD



The core of ObjectSpaces is the three XML files used to describe schemas:

A relational schema definition that describes the structure of a database table, an object schema definition that describes the entity class structure, and a mapping schema that describes the relationship between table structure and entity class mappings Definition.

The main (and most annoying) job of objectspace is to write these three schemas. There is only one XML file to write in the EAP edition, now write three L.



Announcementrsd.xml

Announcementosd.xml

Announcementmsd.xml



Iv. rewriting the DAL



Create a new class file AnnouncementOSDB.cs, containing a new class Announcementosdb, where the method signature is in contrast to the IBuySpy original Announcementdb class. The original announcementdb is used ado.net, return dataset, DataReader, our announcementosdb with ObjectSpaces, return entity Collection class or entity class.



Return announcement Collection according to ModuleID:

Public announcementcollection getannouncements (int moduleid)

{

ObjectSpace OS = new ObjectSpace (_smapfilepath, _conn);

Condition is ModuleID equals argument moduleid,expiredate is greater than current time

ObjectQuery query = new ObjectQuery (typeof (announcement), "ModuleID =" + moduleid.tostring () + "and ExpireDate > #" + DateTime.Now.ToString () + "#");

Fetch data

ObjectReader reader = OS. Getobjectreader (query);



Announcementcollection result = new Announcementcollection ();

Taking a value from a objectreader doesn't require a different shape.

foreach (Announcement ann in reader)

{

Result. Add (ANN);

}



return result;

}



Returns a announcement based on a parameter:

Public Announcement getsingleannouncement (int itemId)

{

ObjectSpace OS = new ObjectSpace (_smapfilepath, _conn);

ObjectQuery query = new ObjectQuery (typeof (announcement), "ItemID =" + itemid.tostring ());

Return (announcement) OS. GetObject (query);

}



Delete a announcement based on the parameter:

public void deleteannouncement (int itemId)

{

ObjectSpace OS = new ObjectSpace (_smapfilepath, _conn);

ObjectQuery query = new ObjectQuery (typeof (announcement), "ItemID =" + itemid.tostring ());

Announcement Ann = (announcement) OS. GetObject (query);

Os. Markfordeletion (ANN);

Os. Persistchanges (ANN);

}



Add a announcement:

public void addannouncement (int moduleid, int itemId, string userName, string title, DateTime expiredate, String descripti On, string morelink, String mobilemorelink)

{

Announcement Ann = new announcement ();

Ann. Setmoduleid (ModuleID);

Ann. Setitemid (-1);

Ann. Createdbyuser = UserName;

Ann. CreatedDate = DateTime.Now;

...



ObjectSpace OS = new ObjectSpace (_smapfilepath, _conn);

Os. Starttracking (Ann, initialstate.inserted);

Os. Persistchanges (ANN);

}



To modify a announcement:

public void updateannouncement (int moduleid, int itemId, string userName, string title, DateTime expiredate, String Descri Ption, String Morelink, String mobilemorelink)

{

ObjectSpace OS = new ObjectSpace (_smapfilepath, _conn);

ObjectQuery query = new ObjectQuery (typeof (announcement), "ItemID =" + itemid.tostring ());

Announcement Ann = (announcement) OS. GetObject (query);



Ann. Createdbyuser = UserName;

Ann. title = title;

...



Os. Persistchanges (ANN);

}



The final modification of the interface layer



The original interface layer is to bind the dataset returned by Announcementdb to Web control, as long as it is changed to bind the Entity collection class returned by ANNOUNCEMENTOSDB to Web control, and the amount of changes is very small.



Like what:

Code that originally took the data out and was bound (in Announcemenets.ascx.cs):

ANNOUNCEMENTSDB announcements = new Announcementsdb ();

Mydatalist.datasource = announcements. Getannouncements (ModuleID);

Mydatalist.databind ();

Just change the first sentence into:

ANNOUNCEMENTOSDB announcements = new Announcementosdb ();

In fact, it is OK to change from which DAL to fetch the data.



Six, talk about ObjectSpaces



The architecture of the objectspaces is this:

ObjectSpace class manages data maps, It is responsible (stealth via Objectengine) to fetch data from the data source (IDbConnection or objectsources) and update the data back to the data source (automatically stealth enabled transaction when updating). It returns a single object via Objectspace.getobject () and returns the Objectset object (this object is similar to a dataset, representing a set of data objects) through Objectspace.getobjectset (). Returns the ObjectReader object (this object, similar to DataReader, is a fast forward-only data Object Reader) by Objectspace.getobjectreader (). It maintains the original value of the data object and changes the value of the monitored data object through the contained ObjectContext.



My code above demonstrates the purpose of showing objectspaces and not having a complete ibuyspy plus a BLL. I also have no demo data between the relations,objectspaces can support very rich relations,onetoone, Manytomany, Onetomany and so on, It also provides lazyloading (which actually takes this data when it really needs to use relation data).



But if you try to rebuild the DAL in your project with objectspaces, I don't know if you feel like me, that's "more trouble than it is now ...". For example, a stored procedure is not supported (is it supported and not mentioned in the document?) EAP version is also supported), hand-written RSD, OSD, msd too cumbersome (there has been a mapper Utility PDC2003, I hope Whidbey will provide automation tools), flexibility to reduce a lot (all ORM framework issues).


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: info-contact@alibabacloud.com 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.