Surance Center

db4o Tutorial 中文翻译(八)

6. 继承


到现在为止,我们已经可以操作实体类了。但是如何操作子类或者接口呢?

为了方便讲解,我们将创造不同种类的sensors
Sensor


TemperatureSensorReadout

PressureSensorReadout




Car的snapshot也要跟着变化:
new car

6.1. 存储


代码只是加入了与sensor的交互而已,其他部分没有动:
// storeFirstCar
Car car1 = new Car("Ferrari");
Pilot pilot1 
= new Pilot("Michael Schumacher"100);
car1.Pilot 
= pilot1;
db.Set(car1);

// storeSecondCar
Pilot pilot2 = new Pilot("Rubens Barrichello"99);
Car car2 
= new Car("BMW");
car2.Pilot 
= pilot2;
car2.Snapshot();
car2.Snapshot();
db.Set(car2);

6.2. 检索


db4o提供所有的给定类型的对象。在检索给定类型的对象时候,不管他们是否是子类或者其他,只要给出一个原型就可以。
// retrieveTemperatureReadoutsQBE
SensorReadout proto = new TemperatureSensorReadout(DateTime.MinValue, nullnull0.0);
IObjectSet result 
= db.Get(proto);
ListResult(result);

// retrieveAllSensorReadoutsQBE
SensorReadout proto = new SensorReadout(DateTime.MinValue, nullnull);
IObjectSet result 
= db.Get(proto);
ListResult(result);



当在如下情况时QBE就不合适了:如果给定的类是一个接口或者抽象类。有一个小诀窍:只要获取对象的类型就可以了
// retrieveAllSensorReadoutsQBEAlternative
IObjectSet result = db.Get(typeof(SensorReadout));
ListResult(result);



当然,还有SODA API:
// retrieveAllSensorReadoutsQuery
IQuery query = db.Query();
query.Constrain(
typeof(SensorReadout));
IObjectSet result 
= query.Execute();
ListResult(result);

6.3. 更新和删除


不管他们在继承树的哪里,他们都和正常的对象一样。.

就像刚刚我们的检索过程, 我们可以删除所有的对象,好进行下一章的讲解。
// deleteAll
IObjectSet result = db.Get(typeof(Object));
foreach (object item in result)
{
    db.Delete(item);
}

6.4. 总结


现在我们已经知道了所有的OO特征如何在db4o里面操作。我们将在下一章完成预排工作,下一章将可以看到深层次的探险,包括循环结构。

6.5. 完整代码

 

锘縰sing System;
using System.IO;
using Db4objects.Db4o;
using Db4objects.Db4o.Query;
namespace Db4objects.Db4o.Tutorial.F1.Chapter4
{   
    
public class InheritanceExample : Util
    
{        
        
public static void Main(string[] args)
        
{
            File.Delete(Util.YapFileName);          
            IObjectContainer db 
= Db4oFactory.OpenFile(Util.YapFileName);
            
try
            
{
                StoreFirstCar(db);
                StoreSecondCar(db);
                RetrieveTemperatureReadoutsQBE(db);
                RetrieveAllSensorReadoutsQBE(db);
                RetrieveAllSensorReadoutsQBEAlternative(db);
                RetrieveAllSensorReadoutsQuery(db);
                RetrieveAllObjects(db);
            }

            
finally
            
{
                db.Close();
            }

        }

        
        
public static void StoreFirstCar(IObjectContainer db)
        
{
            Car car1 
= new Car("Ferrari");
            Pilot pilot1 
= new Pilot("Michael Schumacher"100);
            car1.Pilot 
= pilot1;
            db.Set(car1);
        }

        
        
public static void StoreSecondCar(IObjectContainer db)
        
{
            Pilot pilot2 
= new Pilot("Rubens Barrichello"99);
            Car car2 
= new Car("BMW");
            car2.Pilot 
= pilot2;
            car2.Snapshot();
            car2.Snapshot();
            db.Set(car2);
        }

        
        
public static void RetrieveAllSensorReadoutsQBE(IObjectContainer db)
        
{
            SensorReadout proto 
= new SensorReadout(DateTime.MinValue, nullnull);
            IObjectSet result 
= db.Get(proto);
            ListResult(result);
        }

        
        
public static void RetrieveTemperatureReadoutsQBE(IObjectContainer db)
        
{
            SensorReadout proto 
= new TemperatureSensorReadout(DateTime.MinValue, nullnull0.0);
            IObjectSet result 
= db.Get(proto);
            ListResult(result);
        }

        
        
public static void RetrieveAllSensorReadoutsQBEAlternative(IObjectContainer db)
        
{
            IObjectSet result 
= db.Get(typeof(SensorReadout));
            ListResult(result);
        }

        
        
public static void RetrieveAllSensorReadoutsQuery(IObjectContainer db)
        
{
            IQuery query 
= db.Query();
            query.Constrain(
typeof(SensorReadout));
            IObjectSet result 
= query.Execute();
            ListResult(result);
        }

        
        
public static void RetrieveAllObjects(IObjectContainer db)
        
{
            IObjectSet result 
= db.Get(new object());
            ListResult(result);
        }

    }

}






posted @ 2007-05-06 12:42  xxp  阅读(1059)  评论(0编辑  收藏  举报
Surance Center