db4o发布7.2,出现.NET 3.5版本,支持LINQ
db4o發(fā)布7.2,出現(xiàn).NET 3.5版本,支持LINQ
Db4Object剛剛發(fā)布了db4o的7.2beta,除了以前支持如下的平臺(tái):.NET 1.1,.NET 2.0,Mono外,現(xiàn)在還支持.NET 3.5了。當(dāng)然支持.NET 3.5,最主要的時(shí)候要來(lái)支持LINQ。
關(guān)于LINQ,我稍后再講。現(xiàn)在講講7.2中最大的新特性——Transparent Activation(透明激活)。關(guān)于7.0版本的其他新特性,可以參看我在InfoQ上的文章《Db4Objects發(fā)布Db4o 7.0,支持透明激活》。
要講到透明激活,我們先來(lái)看看之前激活存在的問(wèn)題。所謂激活,就是在對(duì)象從磁盤文件載入到內(nèi)存過(guò)程中,如何加載層級(jí)對(duì)象的過(guò)程。由于對(duì)象的層級(jí)關(guān)系可以無(wú)限關(guān)聯(lián)的,所以,db4o之前使用“深度”的概念來(lái)明確表明處理對(duì)象的時(shí)候需要,處理到多少層。但是,這種方式對(duì)編程帶來(lái)很多麻煩,我們?cè)趯?xiě)代碼的時(shí)候需要隨時(shí)關(guān)心,我們大概要加載多深的對(duì)象。這樣的估計(jì)有時(shí)候會(huì)比要使用到的多,有時(shí)候又會(huì)少。多了,造成資源的浪費(fèi),少了,不能正確的處理需要處理到的對(duì)象。
下面我引用,db4o文檔中對(duì)這個(gè)問(wèn)題的描述(英文的我就不翻譯了):
We can reuse most of the code from the?Deep Graphs chapter?and get it to work with Transparent Activation.
As a first step we should fill up our database with Car, Pilot and SensorReadout objects, so we have some objects to work with.
// storeCarAndSnapshots
Pilot pilot = new Pilot("Kimi Raikkonen", 110);
Car car = new Car("Ferrari");
car.Pilot = pilot;
for (int i = 0; i < 5; i++)
{
??? car.snapshot();
}
db.Store(car);
If we now rerun the code to traverse all cars and their sensor readings, we are again confronted with the same problem that we had before, we end up with some leaves of our object graph being null.??
// retrieveSnapshotsSequentially
IObjectSet result = db.QueryByExample(typeof (Car));
Car car = (Car) result.Next();
SensorReadout readout = car.History;
while (readout != null)
{
??? Console.WriteLine(readout);
??? readout = readout.Next;
}
為了解決這個(gè)問(wèn)題,db4o在7.0中提出了透明激活的概念,即db4o透明地幫我們處理對(duì)象激活的問(wèn)題。這樣可以提供性能,讓我們編程更方便。
再次引用db4o文檔的代碼:
Let's configure db4o to run in Transparent Activation mode and let's try again:
// configureTransparentActivation
Db4oFactory.Configure().Add(new TransparentActivationSupport());
// retrieveSnapshotsSequentially
IObjectSet result = db.QueryByExample(typeof (Car));
Car car = (Car) result.Next();
SensorReadout readout = car.History;
while (readout != null)
{
??? Console.WriteLine(readout);
??? readout = readout.Next;
}
Wow it worked! Is it really that easy? Principally yes. When db4o is run in Transparent Activation mode there are no surprises with null members that have not yet been read from the database.
好了,透明激活就講到這里,現(xiàn)在來(lái)看看LINQ的支持。Linq在7.0發(fā)布之前,在db4o上已經(jīng)有一個(gè)linq to db4o的項(xiàng)目在做前期研究了,現(xiàn)在只是把linq to db4o合并到7.2中一起發(fā)布。
linq to db4o提供了一個(gè)額外的程序集:Db4objects.Db4o.Linq
要使用linq to db4o,只需要在項(xiàng)目中引用這個(gè)程序集。然后打開(kāi)一個(gè)db4o數(shù)據(jù)庫(kù),就可以使用linq語(yǔ)法查詢數(shù)據(jù)了:
Let's prepare some objects in our database to query against:
// storeObjects
db.Store(new Car("Ferrari", (new Pilot("Michael Schumacher", 100))));
db.Store(new Car("BMW", (new Pilot("Rubens Barrichello", 99))));
The simplest LINQ query will look like this:
// retrievePilot
IEnumerable<Pilot> result = from Pilot p in db
??????????????????????????? where p.Name.StartsWith("Michael")
??????????????????????????? select p;
ListResult(result);
You can see that we are using db4o object container as a datasource, the rest of the syntax is generic to all LINQ queries.
Now let's try a bit more complex selection:
// retrievePilotByCar
IEnumerable<Pilot> result = from Car c in db
??????????????????????????? where c.Model.StartsWith("F")
??????????????????????????? && (c.Pilot.Points > 99 && c.Pilot.Points <150)
??????????????????????????? select c.Pilot;
ListResult(result);
另外這里,也有一個(gè)linq to db4o的例子:Linq is here!
通過(guò)linq來(lái)查詢db4o確實(shí)帶來(lái)了很多方便,不過(guò)現(xiàn)在linq to db4o還沒(méi)有非常成熟,期待其更加完善成熟。
大家對(duì)db4o 7.2有興趣的,可以到這里下載來(lái)試試。
轉(zhuǎn)載:http://www.cnblogs.com/redmoon/archive/2008/02/23/1078619.html轉(zhuǎn)載于:https://www.cnblogs.com/tianciliangen/p/6827985.html
總結(jié)
以上是生活随笔為你收集整理的db4o发布7.2,出现.NET 3.5版本,支持LINQ的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle查看日期是第几周,oracl
- 下一篇: PMBOK十大知识领域及其管理过程