我正在尝试将 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/