开发者

db4o Tranparent Persistence doesn't store later objects in my own ActivatableCollection<T>

I'm rolling my own ActivatableCollection<T> for db4o but cribbing heavily from the builtin ActivatableList<T> implementation. I'm running into the problem where transparent persistence doesn't seem to be working correctly. In the test code below:

[Fact]
void CanStoreActivatableCollection()
{
    var planets = new ActivatableCollection<Planet>();

    var pagingMemoryStorage = new PagingMemoryStorage();
    var config = Db4oEmbedded.NewConfiguration();
    config.Common.Add(new TransparentActivationSupport());
    config.Common.Add(new TransparentPersistenceSupport());
    config.File.Storage = pagingMemoryStorage;

    var objectContainer = Db4oEmbedded.OpenFile(config, "Memory.yap");

    planets.Add(new Planet("Mercury"));

    objectContainer.Store(planets);

    planets.Add(new Planet("Venus"));
    planets.Add(new Planet("Earth"));

    objectContainer.Commit();
    objectContainer.Close();

    config = Db4oEmbedded.NewConfiguration();
    config.Common.Add(new TransparentActivationSupport());
    config.Common.Add(new TransparentPersistenceSupport());
    config.File.Storage = pagingMemoryStorage;

    objectContainer = Db4oEmbedded.OpenFile(config, "Memory.yap");
    planets = objectContainer.Query<ActivatableCollection<Planet>>().FirstOrDefault();
    Assert.NotNull(planets);
    Assert.Equal(3, planets.Count);
    objectContainer.Close();
}

The planet "Mercury" is stored, but not "Venus" and "Earth". If I change from ActivatableCollection to ActivatableList, then all 3 planets are stored.

What am I missing? My ActivatableCollection is just minimal implementation of ActivatableList as best as I can tell.

Below is my implementation of ActivatableCollection:

public class ActivatableCollection<T>
    : ICollection<T>
    , IActivatable
    , INotifyCollectionChanged
{
    List<T> _list;

    List<T> List
    {
        get
        {
            if (_list == null)
                _list = new List<T>();
            return _list;
        }
    }

    public ActivatableCollection()
    {
    }

    public int Count
    {
        get
        {
            ActivateForRead();
            return List.Count;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            ActivateForRead();
            return ((IList) List).IsReadOnly;
        }
    }

    public void Add(T t)
    {
        ActivateForWrite();
        List.Add(t);
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, t));
    }

    public void Clear()
    {
        ActivateForWrite();
        List.Clear();
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public bool Contains(T t)
    {
        ActivateForRead();
        return List.Contains(t);
    }

    public void CopyTo(T[] array, int index)
    {
        ActivateForRead();
        List.CopyTo(array, index);
    }

    public IEnumerator<T> GetEnumerator()
    {
        ActivateForRead();
        return List.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnum开发者_运维技巧erator()
    {
        return GetEnumerator();
    }

    public bool Remove(T t)
    {
        ActivateForWrite();
        bool removed = List.Remove(t);
        if (removed)
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t));
        return removed;
    }

    [Transient]
    private IActivator _activator;

    public virtual void Bind(IActivator activator)
    {
        if (_activator == activator)
            return;
        if (activator != null && _activator != null)
            throw new InvalidOperationException();
        _activator = activator;
    }

    public virtual void Activate(ActivationPurpose purpose)
    {
        if (_activator == null)
            return;
        _activator.Activate(purpose);
    }

    protected virtual void ActivateForRead()
    {
        Activate(ActivationPurpose.Read);
    }

    protected virtual void ActivateForWrite()
    {
        Activate(ActivationPurpose.Write);
    }

    [Transient]
    public event NotifyCollectionChangedEventHandler CollectionChanged;

    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (CollectionChanged != null)
            CollectionChanged(this, e);
    }
}

I've also tried copying the code from GenericTypeHandlerPredicate and registering my ActivatableCollection to use the GenericCollectionTypeHandler. That results in a crash in GenericTypeFor() throwing an InvalidOperationException() when "Mercury" is being stored.


Just want to mention my answers from the db4o forums also here, for people with a similar problem:

First part of the issue: From db4o's point of view nothing has changed in the 'ActivatableCollection' object and therefore no changes are stored. This is what is happening:

  1. When you add the items, the ActivatableCollection is marked as changed.

  2. When you commit the changes are stored. However the ' ActivatableCollection' holds the reference to the same object. db4o only stores the changes in the ActivatableCollection-object, which is the reference to the List. Since it is the same, no actual change is stored.

  3. The List of the ActivatableCollection is never updated, because it wasn't marked as 'changed'

So the transparent activation doesn't see the changes in the list. You can fix your issue simply by using an ActivatableList in you're ActivatableCollection implementation. Just change the List with a IList interface and instantiate a ActivatableList instead of an List.

The second part of the issue: Why doesn't it work even when registering the GenericCollectionTypeHandler for this type? Here we hit a implementation detail. The GenericCollectionTypeHandler has an internal list of supported types, which doesn't include the self made 'ActivatableCollection'. GenericCollectionTypeHandler is not really part of the public API and intendet for internal use only.

Workaround / Fix

Just use an ActivatableList<T> instead of a List<T>. then everything works fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜