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文件来映射对象和数据库的关系。

 1<map xmlns=" http://www.microsoft.com/ObjectSpaces-v1 ">
 2<type datasource="customers" name="Customer" source="s">
 3<uniquekey datasource="CustomerId" mappedby="autoIncrement" name="Id"></uniquekey>
 4<property datasource="ContactName" name="Name"></property>
 5<property datasource="ContactTitle" name="Title"></property>
 6<property datasource="CompanyName" name="Company"></property>
 7<property datasource="Phone" name="Phone"></property>
 8<property datasource="Fax" name="Fax"></property>
 9<method name="GetRandomOrder" userassembly="Customization.dll" userclass="Customizer"></method>
10</type></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

Published At
Categories with Web编程
Tagged with
comments powered by Disqus