开发者

db4o getting history of container

开发者 https://www.devze.com 2023-03-16 01:18 出处:网络
var config = Db4oEmbedded.NewConfiguration (); using (var container = Db4oEmbedded.OpenFile (config, FILE))
        var config = Db4oEmbedded.NewConfiguration ();
        using (var container = Db4oEmbedded.OpenFile (config, FILE))
        {
            var foo = new Foo ("Test");
            container.Store (foo);

            foo.Name = "NewName";
            container.Store (foo);
        }

Any way to resolve the history of container for开发者_如何学运维 foo in the format below?

Foo created with values "Test" Foo

Foo's property "Test" changed to "NewName"


You can do by implementing event-handlers. Basically you can register a event-handler for the creating and the updating event. Like this:

IEventRegistry events = EventRegistryFactory.ForObjectContainer(container);
events.Creating +=delegate(object sender, CancellableObjectEventArgs args)
    {
        Console.WriteLine("{0} created: Value {1}",args.Object.GetType(),args.Object);
    };

For viewing value changes you maybe need to peek the old state in the event-handler. You can do this like this:

IEventRegistry events = EventRegistryFactory.ForObjectContainer(container);
events.Creating +=delegate(object sender, CancellableObjectEventArgs args)
    {
        IObjectContainer eventContainer = args.ObjectContainer();
        object oldVersion = eventContainer.Ext().Peek(args.Object,1,false);
        object currentVersion = args.Object;

        // Do comparison and print stuff
    };

Of course the comparison and printing is the work you have to do. There's nothing built in for that.

0

精彩评论

暂无评论...
验证码 换一张
取 消