使用Fluent-NHibernate和WPF:是否有约定将uNhAddIns…ObservableListType<T>作为默认类型?

3

我正在尝试在需要Observable集合(实现 INotifyCollectionChanged 接口)的WPF中使用Fluent-Nibernate。

uNHAddins:NHibernate的非官方附加组件上,我发现了

    uNhAddIns.WPF.Collections.Types.ObservableListType<T>

实现了 INotifyCollectionChanged 接口。可以在 Fluent-Nibernate 中进行如下配置:

    namespace FluentNHibernateTutorial.Mappings
    {
        public class StoreMap : ClassMap<Store>
        {
            public StoreMap()
            {
                Id(x => x.Id);
                Map(x => x.Name);
                HasManyToMany(x => x.Products)
                 .CollectionType<uNhAddIns.WPF.Collections.Types
                                      .ObservableListType<Product>>()
                 .Cascade.All()
                 .Table("StoreProduct");
            }
        }
    }

有人知道如何使用Fluent-NHibernate实现一个Convention,始终将ObservableListType作为默认的IList实现吗?

更新:完美的解决方案应该是使用Fluent-NHibernate-Automapper进行替换。

1个回答

7
像这样的东西应该可以解决问题:
public class ObservableListConvention :
    IHasManyConvention, IHasManyToManyConvention, ICollectionConvention {

    // For one-to-many relations
    public void Apply(IOneToManyCollectionInstance instance) {

        ApplyObservableListConvention(instance);
    }

    // For many-to-many relations
    public void Apply(IManyToManyCollectionInstance instance) {

        ApplyObservableListConvention(instance);
    }

    // For collections of components or simple types
    public void Apply(ICollectionInstance instance) {

        ApplyObservableListConvention(instance);
    }

    private void ApplyObservableListConvention(ICollectionInstance instance) {

        Type collectionType =
            typeof(uNhAddIns.WPF.Collections.Types.ObservableListType<>)
            .MakeGenericType(instance.ChildType);
        instance.CollectionType(collectionType);
    }
}

针对问题更新的回答:

这个约定应该可以与automapper一起使用,如下所示:

AutoMap.AssemblyOf<Store>(cfg)
  .Conventions.Add<ObservableListConvention>();

+1 这对我很有效。我删除了之前在这个答案下的评论,因为你相应地更新了你的答案。非常感谢。 - k3b
很高兴听到这个好消息。我很高兴能够帮助。 - Yhrn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接