wpf - Fluent-Nibernate 以 wpf : Convention to use uNhAddIns. ..ObservableListType<T> 作为默认值?

标签 wpf collections fluent-nhibernate nhibernate-mapping nhibernate-collections

我正在尝试将 Fluent-Nibernate 与需要 Observable 集合的 wpf 一起使用(实现 INotifyCollectionChanged 接口(interface))。

uNHAddins: Unofficial addins for 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-Nibernate 来实现约定,并且总是使用 ObservableListType 作为默认 IList 实现吗?

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

最佳答案

像这样的事情应该可以解决问题:

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);
    }
}

针对问题更新的回应:

这个约定应该与自动映射器一起使用,如下所示:

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

关于wpf - Fluent-Nibernate 以 wpf : Convention to use uNhAddIns. ..ObservableListType<T> 作为默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4877338/

相关文章:

c# - Datagrid.RowEditEnding 不返回更新值

java - 如何将 "Pair"或 n 大小插入列表集合而不是组成 HashMap?

c# - 如何配置 Fluent NHibernate 以将查询输出到 Trace 或 Debug 而不是控制台?

nhibernate - Fluent Nhibernate - HasMany - 未保存子对象

c# - 特定类型的数据触发器

c# - 使用 WPF 工具包的 WPF 堆叠条形图

c# - 如何根据绑定(bind)对象的类型动态更改DataTemplate?

javascript - 使用纯 JavaScript,给定一个对象集合和一个过滤器对象,返回集合中与过滤器对象具有相同键/值对的对象

java - 使用 Collections 或我的函数计算 ArrayList 中对象的出现次数

nhibernate - Oracle 托管驱动程序 ODP.NET 与 NHibernate 4.0 FLOAT (126) 到 C# DECIMAL/Double