天天看點

Microsoft ObjectSpaces(O/R Mapping)

Microsoft ObjectSpaces

objectspaces是個orm 工具,這個orm應該是object-relational mapping 而不是object rule modal,其主要目的就是為了簡化資料庫和對象之間的映射。連接配接 http://groups.msn.com/objectspaces

objectspaces類似于jdo(jdo是java的一個orm工具),在pdc版本中,提供了XmlObjectSpace 和SqlObjectSpace兩個類,支援xml或是Sql Server。

從技術角度來說,objectspaces建築于ado.net之上(同jdo建築在jdbc之上一樣)一樣,使用map檔案來映射對象和資料庫的關系。

<map xmlns="http://www.microsoft.com/ObjectSpaces-v1">

  <type name="Customer" dataSource="customers" source="s">

    <uniqueKey name="Id" mappedBy="autoIncrement" dataSource="CustomerId" />

    <property name="Name" dataSource="ContactName" />

    <property name="Title" dataSource="ContactTitle"/>

    <property name="Company" dataSource="CompanyName"/>

    <property name="Phone" dataSource="Phone"/>

    <property name="Fax" dataSource="Fax"/>

    <method name="GetRandomOrder" userAssembly="Customization.dll" userClass="Customizer" />

</map>

在這個map檔案中,type tag中的name是objectspaces中的對象,datasource 則是資料庫表名,而source則是資料源map檔案中定義的資料源

property或是uniqueKey對應一個字段或是關鍵字字段。大家可以看到uniqueKey中的mappedBy被設定為autoIncrement,意味着是sql server的identity或是access中的自動計數字段。當然,mappedBy還有guid、user或是custom等多種方式。dataSource則是實際的字段名。

大家可以粗略看出這種定義方式非常靈活,因為在設計初期,資料庫會經常改變,這種定義方式,可以減少對源代碼的改動。

使用objectspaces可以大大簡化你的資料庫存儲代碼,其實objectspaces的原則就是讓你:

不使用 sql 語言

不使用存儲過程

在objectSpaces中,資料庫操作(新增、删除、更新、查詢)被簡略成CreateObject,Delete,Update(UpdateAll),getobject(getObjects)等幾個方法,下面以一個執行個體來介紹。

如一個Customer(客戶) 對象,其可能有Id(編号),Name( 名稱),Address(位址)等屬性

注:代碼中的os是ObjectSpace的一個執行個體

定義對象

public abstract Customer

{

[UniqueId]

public abstract Int32 Id{get;set;}

public abstract String Name{get;set;}

public abstract String Address{get;set;}

public void OnCreate(Int32 ItemId,String ItemName,String ItemAddress)

{

this.Id=ItemId;

this.Name=ItemName;

this.Address=ItemAddress;

}

}

查詢對象

GetObject

GetObjects

GetObject傳回單一對象,GetObjects傳回對象的集合(ObjectSet)

查詢使用的是(OPath,ObjectPathLanguage),也就是使用對象.屬性的方式進行查詢,查詢根據程式設計語言的不同而不同,如C#用==表示是否相等而vb.net則用=

c=os.GetObject(typeof(Customer),"Id==1");

注:(在pdc版本中,like 操作還未被支援,正式版中會支援這個操作,而且,從新聞討論區的一些文字上來看,這個特性已經被實作)

objectSpace使用如下方式進行新增,删除、更新操作

新增對象

CreateObject

Customer c=os.CreateObject(typeof(Customer),1,"jjx","浙江省")

删除對象

DeleteObject

更新

Update 更新某個對象

UpdateAll 更新全部對象

objectSpaces中的對象定義可以自行擴充,以加入各種商業邏輯。請大家參閱幫助檔案。

老早就聽到過這個東西,但沒有找到,這次在fabrice's 的webblog(http://dotnetweblogs.com/FMARGUERIE/)中發現了這個連接配接(http://groups.msn.com/objectspaces)

現在的objectspaces還是最早的pdc preview版本,在浏覽了新聞討論區和其它網站的一些消息後,确定這将是一個不會發表的産品,不過objectspaces 将會融合在.net 2.0中,來源如下

Latest Update on Microsoft ObjectSpaces

Wednesday, October 02, 2002 9:14 PM

According to Guang-an Wu who works for the company, Microsoft have utilized the feedback received via newsgroups and through customer meetings to improve ObjectSpaces further. Currently, ObjectSpaces is being developed as a part of the next major .NET framework release.

Stay tuned for further announcements at the PDC next year.

Since the component is still under development, many of the details are still being decided and are not yet available for public release. However, here are some of the highlights, with the usual caveat that all details are subject to change without further notice. And Additional details beyond these highlights are not available at this point for release.

Unlike the PDC 2001 Technology Preview, classes don't need to be abstract. The goal is to make ObjectSpaces available for all .NET objects.

The foundation is a better optimized streaming model for fast retrieval of objects.

Key features like simplicity of OPath queries for retrieving objects, choice of span or delay loading are being enhanced and improved.

More sophisticated support is planned for inheritance and advanced mapping between classes and one or more tables.

Further, tools support for application development lifecycle is in the works.

雖然objectspaces隻是個預覽産品,但其特征非常豐富,我非常喜歡其基于map檔案的特性,這是 ado.net的TableMappings集合的檔案化,使你在改動資料庫(如改變字段名時)不必改動代碼。

缺點

1、對象查詢速度

我做了一下測試,查詢一個8000條的包含7個屬性的對象需要20秒

2、綁定

ObjectSet實作支援ICollection和支援IBindingList,是以支援綁定

類自己定義,需要自己實作IEditableObject,IDataErrorInfo來完美的支援.net資料綁定

約定

在OjbectSpace中

對象的UniqueId屬性是必須的,當在進行OPaht查詢時需要使用

posted on 2003-4-18 21:52:44

Comments

No comments posted yet