db4o Tutorial 中文翻译(七)

5. 集合和数组


我们将要通过增加Sensor类到Car类,来慢慢转移到实时数据处理

ContractedBlock.gif ExpandedBlockStart.gif Sensor类
None.gifusing System;
None.gif
using System.Text;
None.gif    
None.gif
namespace Db4objects.Db4o.Tutorial.F1.Chapter3
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{   
InBlock.gif    
public class SensorReadout
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
double[] _values;
InBlock.gif        DateTime _time;
InBlock.gif        Car _car;
InBlock.gif        
InBlock.gif        
public SensorReadout(double[] values, DateTime time, Car car)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _values 
= values;
InBlock.gif            _time 
= time;
InBlock.gif            _car 
= car;
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 int NumValues
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _values.Length;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public double[] Values
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _values;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public double GetValue(int idx)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return _values[idx];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
override public string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder builder 
= new StringBuilder();
InBlock.gif            builder.Append(_car);
InBlock.gif            builder.Append(
" : ");
InBlock.gif            builder.Append(_time.TimeOfDay);
InBlock.gif            builder.Append(
" : ");
InBlock.gif            
for (int i=0; i<_values.Length; ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (i > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    builder.Append(
"");
ExpandedSubBlockEnd.gif                }

InBlock.gif                builder.Append(_values[i]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return builder.ToString();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

一个Car在需要记录他的比赛情况的时候,可以产生他目前的Sensor Readout :

ContractedBlock.gif ExpandedBlockStart.gif 更新的car类
None.gifusing System;
None.gif
using System.Collections;
None.gif    
None.gif
namespace Db4objects.Db4o.Tutorial.F1.Chapter3
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) : this(model, new ArrayList())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public Car(string model, IList history)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _model 
= model;
InBlock.gif            _pilot 
= null;
InBlock.gif            _history 
= history;
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 IList History
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _history;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void Snapshot()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _history.Add(
new SensorReadout(Poll(), DateTime.Now, this));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
protected double[] Poll()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int factor = _history.Count + 1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
return new double[] dot.gif0.1d*factor, 0.2d*factor, 0.3d*factor };
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

我们现在只要加静态的数据,下一个章节就要加入更灵活的数据了。


5.1. 存储数据


现在你应该很熟悉这个步骤了吧!
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);

第二个Car我们要加入2个snapshots:
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);

5.2. 检索



5.2.1. QBE


首先,要确定snapshots是否被真的加入了.
None.gif //  retrieveAllSensorReadout
None.gif
IObjectSet result  =  db.Get( typeof (SensorReadout));
None.gifListResult(result);
注意:原型中给定元素的实际位置是错的。
为了根据一个car的sensor readouts来检索car,我们要记录查看历史
None.gif //  retrieveCarQBE
ExpandedBlockStart.gifContractedBlock.gif
SensorReadout protoReadout  =   new  SensorReadout( new   double []  dot.gif 0.60.2 } , DateTime.MinValue,  null );
None.gifIList protoHistory 
=   new  ArrayList();
None.gifprotoHistory.Add(protoReadout);
None.gifCar protoCar 
=   new  Car( null , protoHistory);
None.gifIObjectSet result 
=  db.Get(protoCar);
None.gifListResult(result);
我们也可以检索集合本身,因为他们也是对象
None.gif //  retrieveCollections
None.gif
IObjectSet result  =  db.Get( new  ArrayList());
None.gifListResult(result);


这对array不起作用:
None.gif //  retrieveArrays
ExpandedBlockStart.gifContractedBlock.gif
IObjectSet result  =  db.Get( new   double []  dot.gif 0.60.4 } );
None.gifListResult(result);

5.2.2. Native Queries


如果你想用 Native Queries 来通过匹配来查找SensorReadouts, 和检索单个值(非集合、数组),是一样的:
None.gif public   class  RetrieveSensorReadoutPredicate : Predicate
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public bool Match(SensorReadout candidate)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return Array.IndexOf(candidate.Values, 0.3> -1
InBlock.gif            
&& Array.IndexOf(candidate.Values, 0.1> -1;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif //  retrieveSensorReadoutNative
None.gif
IObjectSet results  =  db.Query( new  RetrieveSensorReadoutPredicate());
None.gifListResult(results);

这里是根据匹配的readout 来查找car:
None.gif public   class  RetrieveCarPredicate : Predicate
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public bool Match(Car car)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
foreach (SensorReadout sensor in car.History)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Array.IndexOf(sensor.Values, 0.3> -1
InBlock.gif                
&& Array.IndexOf(sensor.Values, 0.1> -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif //  retrieveCarNative
None.gif
IObjectSet results  =  db.Query( new  RetrieveCarPredicate());
None.gifListResult(results);

5.2.3. 检索 API


对arrays and collections操作类似上面的例子. 首先,我们只用匹配值检索 SensorReadouts :
// retrieveSensorReadoutQuery
IQuery query = db.Query();
query.Constrain(typeof(SensorReadout));
IQuery valueQuery = query.Descend("_values");
valueQuery.Constrain(0.3);
valueQuery.Constrain(0.1);
IObjectSet results = query.Execute();
ListResult(results);


然后,我们根据匹配的 Readout 检索car:
None.gif //  retrieveCarQuery
None.gif
IQuery query  =  db.Query();
None.gifquery.Constrain(
typeof (Car));
None.gifIQuery historyQuery 
=  query.Descend( " _history " );
None.gifhistoryQuery.Constrain(
typeof (SensorReadout));
None.gifIQuery valueQuery 
=  historyQuery.Descend( " _values " );
None.gifvalueQuery.Constrain(
0.3 );
None.gifvalueQuery.Constrain(
0.1 );
None.gifIObjectSet results 
=  query.Execute();
None.gifListResult(results);

5.3. 更新和删除


应该很熟悉了吧,我们只需要设置car的更新深度:
None.gif //  updateCarPart1
None.gif
Db4oFactory.Configure().ObjectClass( typeof (Car)).CascadeOnUpdate( true );

None.gif //  updateCarPart2
None.gif
IObjectSet result  =  db.Get( new  Car( " BMW " null ));
None.gifCar car 
=  (Car)result.Next();
None.gifcar.Snapshot();
None.gifdb.Set(car);
None.gifRetrieveAllSensorReadouts(db);

当删除arrays and collections时候没有什么不同的。
从一个集合中删除对象也是一样的。
None.gif //  updateCollection
None.gif
IQuery query  =  db.Query();
None.gifquery.Constrain(
typeof (Car));
None.gifIObjectSet result 
=  query.Descend( " _history " ).Execute();
None.gifIList history 
=  (IList)result.Next();
None.gifhistory.RemoveAt(
0 );
None.gifdb.Set(history);
None.gifCar proto 
=   new  Car( null null );
None.gifresult 
=  db.Get(proto);
None.gif
foreach  (Car car  in  result)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {    
InBlock.gif    
foreach (object readout in car.History)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(readout);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



(通过这个例子,我们可以看到db4o可以很容易进入对象内部,这在以前是很少见的。请牢记以上并且小心应用。.)

在进入下一章之前,删除所有的car:
None.gif //  deleteAllPart1
None.gif
Db4oFactory.Configure().ObjectClass( typeof (Car)).CascadeOnDelete( true );

None.gif //  deleteAllPart2
None.gif
IObjectSet result  =  db.Get( new  Car( null null ));
None.gif
foreach  ( object  car  in  result)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    db.Delete(car);
ExpandedBlockEnd.gif}

None.gifIObjectSet readouts 
=  db.Get( new  SensorReadout( null , DateTime.MinValue,  null ));
None.gif
foreach  ( object  readout  in  readouts)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    db.Delete(readout);
ExpandedBlockEnd.gif}

5.4. 总结


Ok, collections也是对象。但是我们怎么定义它的类型呢?有没有必要定义呢? db4o如何处理继承?下一章节将讲到。


5.5. 全部代码

 

None.gif 锘縰sing System;
None.gif
using  System.Collections;
None.gif
using  System.IO;
None.gif
using  Db4objects.Db4o;
None.gif
using  Db4objects.Db4o.Query;
None.gif
namespace  Db4objects.Db4o.Tutorial.F1.Chapter3
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {    
InBlock.gif    
public class CollectionsExample : 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                RetrieveAllSensorReadouts(db);
InBlock.gif                RetrieveSensorReadoutQBE(db);
InBlock.gif                RetrieveCarQBE(db);
InBlock.gif                RetrieveCollections(db);
InBlock.gif                RetrieveArrays(db);
InBlock.gif                RetrieveSensorReadoutQuery(db);
InBlock.gif                RetrieveCarQuery(db);
InBlock.gif                db.Close();
InBlock.gif                UpdateCarPart1();
InBlock.gif                db 
= Db4oFactory.OpenFile(Util.YapFileName);
InBlock.gif                UpdateCarPart2(db);
InBlock.gif                UpdateCollection(db);
InBlock.gif                db.Close();
InBlock.gif                DeleteAllPart1();
InBlock.gif                db
=Db4oFactory.OpenFile(Util.YapFileName);
InBlock.gif                DeleteAllPart2(db);
InBlock.gif                RetrieveAllSensorReadouts(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 RetrieveAllSensorReadouts(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 RetrieveSensorReadoutQBE(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            SensorReadout proto 
= new SensorReadout(new double[] dot.gif0.30.1 }, DateTime.MinValue, null);
InBlock.gif            IObjectSet result 
= db.Get(proto);
InBlock.gif            ListResult(result);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void RetrieveCarQBE(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            SensorReadout protoReadout 
= new SensorReadout(new double[] dot.gif0.60.2 }, DateTime.MinValue, null);
InBlock.gif            IList protoHistory 
= new ArrayList();
InBlock.gif            protoHistory.Add(protoReadout);
InBlock.gif            Car protoCar 
= new Car(null, protoHistory);
InBlock.gif            IObjectSet result 
= db.Get(protoCar);
InBlock.gif            ListResult(result);
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        
InBlock.gif        
public static void RetrieveArrays(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            IObjectSet result 
= db.Get(new double[] dot.gif0.60.4 });
InBlock.gif            ListResult(result);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void RetrieveSensorReadoutQuery(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IQuery query 
= db.Query();
InBlock.gif            query.Constrain(
typeof(SensorReadout));
InBlock.gif            IQuery valueQuery 
= query.Descend("_values");
InBlock.gif            valueQuery.Constrain(
0.3);
InBlock.gif            valueQuery.Constrain(
0.1);
InBlock.gif            IObjectSet results 
= query.Execute();
InBlock.gif            ListResult(results);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void RetrieveCarQuery(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IQuery query 
= db.Query();
InBlock.gif            query.Constrain(
typeof(Car));
InBlock.gif            IQuery historyQuery 
= query.Descend("_history");
InBlock.gif            historyQuery.Constrain(
typeof(SensorReadout));
InBlock.gif            IQuery valueQuery 
= historyQuery.Descend("_values");
InBlock.gif            valueQuery.Constrain(
0.3);
InBlock.gif            valueQuery.Constrain(
0.1);
InBlock.gif            IObjectSet results 
= query.Execute();
InBlock.gif            ListResult(results);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public class RetrieveSensorReadoutPredicate : Predicate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public bool Match(SensorReadout candidate)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Array.IndexOf(candidate.Values, 0.3> -1
InBlock.gif                    
&& Array.IndexOf(candidate.Values, 0.1> -1;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void RetrieveSensorReadoutNative(IObjectContainer db) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IObjectSet results 
= db.Query(new RetrieveSensorReadoutPredicate());
InBlock.gif            ListResult(results);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public class RetrieveCarPredicate : Predicate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public bool Match(Car car)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (SensorReadout sensor in car.History)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (Array.IndexOf(sensor.Values, 0.3> -1
InBlock.gif                        
&& Array.IndexOf(sensor.Values, 0.1> -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return true
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void RetrieveCarNative(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IObjectSet results 
= db.Query(new RetrieveCarPredicate());
InBlock.gif            ListResult(results);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void UpdateCarPart1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Db4oFactory.Configure().ObjectClass(
typeof(Car)).CascadeOnUpdate(true);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void UpdateCarPart2(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IObjectSet result 
= db.Get(new Car("BMW"null));
InBlock.gif            Car car 
= (Car)result.Next();
InBlock.gif            car.Snapshot();
InBlock.gif            db.Set(car);
InBlock.gif            RetrieveAllSensorReadouts(db);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void UpdateCollection(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IQuery query 
= db.Query();
InBlock.gif            query.Constrain(
typeof(Car));
InBlock.gif            IObjectSet result 
= query.Descend("_history").Execute();
InBlock.gif            IList history 
= (IList)result.Next();
InBlock.gif            history.RemoveAt(
0);
InBlock.gif            db.Set(history);
InBlock.gif            Car proto 
= new Car(nullnull);
InBlock.gif            result 
= db.Get(proto);
InBlock.gif            
foreach (Car car in result)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
foreach (object readout in car.History)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(readout);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void DeleteAllPart1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Db4oFactory.Configure().ObjectClass(
typeof(Car)).CascadeOnDelete(true);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void DeleteAllPart2(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IObjectSet result 
= db.Get(new Car(nullnull));
InBlock.gif            
foreach (object car in result)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                db.Delete(car);
ExpandedSubBlockEnd.gif            }

InBlock.gif            IObjectSet readouts 
= db.Get(new SensorReadout(null, DateTime.MinValue, null));
InBlock.gif            
foreach (object readout in readouts)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                db.Delete(readout);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值