db4o发布7.2,出现.NET 3.5版本,支持LINQ
db4o發(fā)布7.2,出現(xiàn).NET 3.5版本,支持LINQ
Db4Object剛剛發(fā)布了db4o的7.2beta,除了以前支持如下的平臺:.NET 1.1,.NET 2.0,Mono外,現(xiàn)在還支持.NET 3.5了。當(dāng)然支持.NET 3.5,最主要的時候要來支持LINQ。
關(guān)于LINQ,我稍后再講。現(xiàn)在講講7.2中最大的新特性——Transparent Activation(透明激活)。關(guān)于7.0版本的其他新特性,可以參看我在InfoQ上的文章《Db4Objects發(fā)布Db4o 7.0,支持透明激活》。
要講到透明激活,我們先來看看之前激活存在的問題。所謂激活,就是在對象從磁盤文件載入到內(nèi)存過程中,如何加載層級對象的過程。由于對象的層級關(guān)系可以無限關(guān)聯(lián)的,所以,db4o之前使用“深度”的概念來明確表明處理對象的時候需要,處理到多少層。但是,這種方式對編程帶來很多麻煩,我們在寫代碼的時候需要隨時關(guān)心,我們大概要加載多深的對象。這樣的估計有時候會比要使用到的多,有時候又會少。多了,造成資源的浪費,少了,不能正確的處理需要處理到的對象。
下面我引用,db4o文檔中對這個問題的描述(英文的我就不翻譯了):
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;
}
為了解決這個問題,db4o在7.0中提出了透明激活的概念,即db4o透明地幫我們處理對象激活的問題。這樣可以提供性能,讓我們編程更方便。
再次引用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)在來看看LINQ的支持。Linq在7.0發(fā)布之前,在db4o上已經(jīng)有一個linq to db4o的項目在做前期研究了,現(xiàn)在只是把linq to db4o合并到7.2中一起發(fā)布。
linq to db4o提供了一個額外的程序集:Db4objects.Db4o.Linq
要使用linq to db4o,只需要在項目中引用這個程序集。然后打開一個db4o數(shù)據(jù)庫,就可以使用linq語法查詢數(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);
另外這里,也有一個linq to db4o的例子:Linq is here!
通過linq來查詢db4o確實帶來了很多方便,不過現(xiàn)在linq to db4o還沒有非常成熟,期待其更加完善成熟。
大家對db4o 7.2有興趣的,可以到這里下載來試試。
轉(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的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle查看日期是第几周,oracl
- 下一篇: PMBOK十大知识领域及其管理过程