不确定DB4O是否提供了这种现成的功能,但是可以自己实现某种悲观锁,这样同一个对象要么不会返回,要么以只读模式返回。您需要维护一个正在编辑的对象列表,并在每次返回对象时检查该列表。但是也有一些问题,比如用户离开,对象被卡在“编辑模式”中等等。你通常需要一种包含超时机制的服务来处理这个问题。事情可能会变得复杂。
编辑
using System;
using System.Collections.Generic;
using System.Linq;
namespace ListTest
{
class Program
{
static void Main(string[] args)
{
DataAccess dataAccess = new DataAccess();
List<MyThing> things = dataAccess.GetThings();
MyThing thingToWorkOn = things[things.Count-1];
printThingList(things);
dataAccess.LockThing(thingToWorkOn);
List<MyThing> moreThings = dataAccess.GetThings();
printThingList(moreThings);
thingToWorkOn.Name = "Harrold";
thingToWorkOn.Name = "Harry";
dataAccess.Save(thingToWorkOn);
dataAccess.UnlockThing(thingToWorkOn);
List<MyThing> evenMoreThings = dataAccess.GetThings();
printThingList(evenMoreThings);
}
static void printThingList(List<MyThing> things)
{
Console.WriteLine("* Things *");
things.ForEach(x => Console.WriteLine(x.Name));
Console.WriteLine();
}
}
class MyThing : IEquatable<MyThing>
{
public string Name { get; set; }
public bool Equals(MyThing other)
{
return other.Name == this.Name;
}
}
class DataAccess
{
private static List<MyThing> lockedThings = new List<MyThing>();
public List<MyThing> GetThings()
{
List<MyThing> thingsFromDatabase = LoadThingsFromDatabase();
var nonLockedThings = (from thing in thingsFromDatabase
where !lockedThings.Contains(thing)
select thing
).ToList<MyThing>();
return nonLockedThings;
}
public void LockThing(MyThing thingToLock)
{
lockedThings.Add(thingToLock);
}
public void UnlockThing(MyThing thingToLock)
{
lockedThings.Remove(thingToLock);
}
public void Save(MyThing thing)
{
}
private List<MyThing> LoadThingsFromDatabase()
{
return new List<MyThing>() {
new MyThing(){ Name="Tom" },
new MyThing(){ Name="Dick" },
new MyThing(){ Name="Harry" }
};
}
}
}