menu

秋梦无痕

一场秋雨无梦痕,春夜清风冻煞人。冬来冷水寒似铁,夏至京北蟑满城。

Avatar

关于ObjectSpaces

M$ .NET Framework 2.0里面提供了一个叫做ObjectSpaces的O/R Mapping工具,现在还没有正式发布,不过也已经可以下载了。这儿有一篇介绍:ADO.NET v2.0: ObjectSpaces Delivers an O/R Mapper,也可以去msdn看看:ObjectSpaces Architecture

好像ObjectSpaces不支持跨事务的Caching,Remoting Integration一般,事件处理也一般,还没有Service Object Model和Security Model,而且微软的东西就那样,只支持SQL Server。对Object Schema的动态扩展和数据库的自定义数据类型都不支持。不知道正式版本推出的时候会怎么样。

下面是与LLBLGen的示例代码相比较,都使用Northwind数据库。
创建:
ObjectSpaces:
Category category = (Category)mapper.GetObject(typeof(Category), "ID=" + this.ID.ToString());

LLBLGen Pro:
CategoryEntity category = new CategoryEntity(this.ID);

更新:
ObjectSpaces(ObjectSpaces似乎必须从数据库中取出具体数据,通常这种动作不是必需的):
category = (Category)mapper.GetObject(typeof(Category), "ID=" + this.ID.ToString());
mapper.StartTracking(category, InitialState.Unchanged);
if (this.CategoryID.Text == "0") {
mapper.MarkForDeletion(category);
}

// ..
category.Name = this.CategoryName.Text;
category.Description = this.CategoryDescription.Text;
mapper.PersistChanges(category);

LLBLGen Pro (采用动态查询引擎,仅仅在数据库上执行更新操作,而不从数据库中读取数据):
CategoryEntity category = new CategoryEntity(); // empty object
category.CategoryID = this.ID;
category.IsNew = false;
category.Description = this.CategoryDescription.Text;
category.Save();

还有,似乎ObjectSpaces必须在进程中保持:
ObjectSpaces:
ObjectSpace mapper = new ObjectSpace (Mapping, connection);
ObjectSet objects = mapper.GetObjectSet(typeof(Category), "");

LLBLGen Pro:
CategoryCollection categories = new CategoryCollection();
categories.GetMulti(null);

与JDO示例代码的比较:
JDO:
PersistenceManagerFactory pmf = ...;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction txn = pm.currentTransaction();
try {
txn.begin();
//从数据库中通过ID取出来Foo对象
Foo foo = pm.getObjectById(new Long(1234), true);
foo.setBlazz("sadfsfsdf");
Bar bar = new Bar();
foo.setBar(bar);
Blah blah = new Blah();
bar.addBlah(blah);
txn.commit();
} catch (Exception x) {
txn.rollback();
throw x;
}

ObjectSpaces:
PersistenceManagerFactory pmf = ...;
PersistenceManager pm = pmf.GetPersistenceManager();
Transaction txn = pm.CurrentTransaction;
try {
txn.Begin();
//从数据库中通过ID取出来Foo对象
Foo foo = pm.GetObjectById(1234, true);
foo.Blazz = "sadfsfsdf";
Bar bar = new Bar();
foo.Bar = bar;
Blah blah = new Blah();
bar.AddBlah(blah);
txn.Commit();
} catch (Exception x) {
txn.Rollback();
throw x;
}

呵呵,好像有些太象了点。而且对于JDO来说,重新加载一个类,直接在内存中替换掉类的bytecode就可以了,java支持这个,但是.net好象就不行了,好像没有ClassLoader这个类,也没有见过可以直接替换bytecode的方法。

评论已关闭