db4o Tutorial 中文翻译(八)

本文介绍了使用Db4o数据库存储和检索具有继承关系的对象。通过实例演示了如何存储Car及其SensorReadout子类,包括TemperatureSensorReadout和PressureSensorReadout,并展示了多种检索方法。

6. 继承


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

为了方便讲解,我们将创造不同种类的sensors
ContractedBlock.gifExpandedBlockStart.gifSensor
None.gifusing System;
None.gif
namespace Db4objects.Db4o.Tutorial.F1.Chapter4
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{   
InBlock.gif    
public class SensorReadout
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DateTime _time;
InBlock.gif        Car _car;
InBlock.gif        
string _description;
InBlock.gif        
InBlock.gif        
public SensorReadout(DateTime time, Car car, string description)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _time 
= time;
InBlock.gif            _car 
= car;
InBlock.gif            _description 
= description;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public Car Car
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _car;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public DateTime Time
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _time;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public string Description
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _description;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
override public string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return string.Format("{0}:{1}:{2}", _car, _time, _description);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


ContractedBlock.gifExpandedBlockStart.gifTemperatureSensorReadout
None.gifusing System;
None.gif
namespace Db4objects.Db4o.Tutorial.F1.Chapter4
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{   
InBlock.gif    
public class TemperatureSensorReadout : SensorReadout
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
double _temperature;
InBlock.gif        
public TemperatureSensorReadout(DateTime time, Car car, string description, double temperature)
InBlock.gif            : 
base(time, car, description)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _temperature 
= temperature;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public double Temperature
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _temperature;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
override public string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return string.Format("{0} temp: {1}"base.ToString(), _temperature);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

ContractedBlock.gifExpandedBlockStart.gifPressureSensorReadout
None.gifusing System;
None.gif
namespace Db4objects.Db4o.Tutorial.F1.Chapter4
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class PressureSensorReadout : SensorReadout
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
double _pressure;
InBlock.gif        
InBlock.gif        
public PressureSensorReadout(DateTime time, Car car, string description, double pressure)
InBlock.gif            : 
base(time, car, description)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _pressure 
= pressure;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public double Pressure
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _pressure;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
override public string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return string.Format("{0} pressure: {1}"base.ToString(), _pressure);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif




Car的snapshot也要跟着变化:
ContractedBlock.gifExpandedBlockStart.gifnew car
None.gifusing System;
None.gif
using System.Collections;
None.gif
namespace Db4objects.Db4o.Tutorial.F1.Chapter4
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{   
InBlock.gif    
public class Car
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string _model;
InBlock.gif        Pilot _pilot;
InBlock.gif        IList _history;
InBlock.gif        
InBlock.gif        
public Car(string model)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _model 
= model;
InBlock.gif            _pilot 
= null;
InBlock.gif            _history 
= new ArrayList();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public Pilot Pilot
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _pilot;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _pilot 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public string Model
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _model;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public SensorReadout[] GetHistory()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SensorReadout[] history 
= new SensorReadout[_history.Count];
InBlock.gif            _history.CopyTo(history, 
0);
InBlock.gif            
return history;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void Snapshot()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _history.Add(
new TemperatureSensorReadout(DateTime.Now, this"oil", PollOilTemperature()));
InBlock.gif            _history.Add(
new TemperatureSensorReadout(DateTime.Now, this"water", PollWaterTemperature()));
InBlock.gif            _history.Add(
new PressureSensorReadout(DateTime.Now, this"oil", PollOilPressure()));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
protected double PollOilTemperature()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return 0.1*_history.Count;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
protected double PollWaterTemperature()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return 0.2*_history.Count;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
protected double PollOilPressure()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return 0.3*_history.Count;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
override public string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return string.Format("{0}[{1}]/{2}", _model, _pilot, _history.Count);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

6.1. 存储


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

None.gif// storeSecondCar
None.gif
Pilot pilot2 = new Pilot("Rubens Barrichello"99);
None.gifCar car2 
= new Car("BMW");
None.gifcar2.Pilot 
= pilot2;
None.gifcar2.Snapshot();
None.gifcar2.Snapshot();
None.gifdb.Set(car2);

6.2. 检索


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

None.gif// retrieveAllSensorReadoutsQBE
None.gif
SensorReadout proto = new SensorReadout(DateTime.MinValue, nullnull);
None.gifIObjectSet result 
= db.Get(proto);
None.gifListResult(result);



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



当然,还有SODA API:
None.gif// retrieveAllSensorReadoutsQuery
None.gif
IQuery query = db.Query();
None.gifquery.Constrain(
typeof(SensorReadout));
None.gifIObjectSet result 
= query.Execute();
None.gifListResult(result);

6.3. 更新和删除


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

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

6.4. 总结


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

6.5. 完整代码

 

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

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                db.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

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

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

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

InBlock.gif        
InBlock.gif        
public static void RetrieveAllSensorReadoutsQBEAlternative(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IObjectSet result 
= db.Get(typeof(SensorReadout));
InBlock.gif            ListResult(result);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void RetrieveAllSensorReadoutsQuery(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IQuery query 
= db.Query();
InBlock.gif            query.Constrain(
typeof(SensorReadout));
InBlock.gif            IObjectSet result 
= query.Execute();
InBlock.gif            ListResult(result);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void RetrieveAllObjects(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IObjectSet result 
= db.Get(new object());
InBlock.gif            ListResult(result);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif





转载于:https://www.cnblogs.com/xxpyeippx/archive/2007/05/06/737027.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值