Nhibernate 使用从列表中删除的属性自动映射类<;T>;
我有一个类,我正在尝试使用Fluent的自动映射功能 该类有一个包含在特殊类中的对象列表,该类包含一个列表。它基本上是通过各种线程来处理列表的自动锁定的 基本上,这里有一个类似的模型:Nhibernate 使用从列表中删除的属性自动映射类<;T>;,nhibernate,fluent-nhibernate,Nhibernate,Fluent Nhibernate,我有一个类,我正在尝试使用Fluent的自动映射功能 该类有一个包含在特殊类中的对象列表,该类包含一个列表。它基本上是通过各种线程来处理列表的自动锁定的 基本上,这里有一个类似的模型: public class Garage { private MutexList<Vehicles> vehicles = new MutexList<Vehicles>(); public virtual MutexList<Vehicles> Vehicles
public class Garage
{
private MutexList<Vehicles> vehicles = new MutexList<Vehicles>();
public virtual MutexList<Vehicles> Vehicles
{
get { return vehicles; }
set { vehicles = value; }
}
}
public class MutexList<T>
{
private List<T> list = new List<T>();
private readonly int timeout = 1000;
public List<T> List
{
get { return list; }
set { list = value; }
}
}
有人能告诉我我必须做什么才能将车库类别与车辆列表中包含的所有车辆进行映射?我正试图在此基础上使用自动映射来生成NHibernate的XML模式,但我不知道如何以这种方式完成类中包含的列表的映射。您无法即时映射自定义集合类 这在以下方面非常清楚: NHibernate要求将持久集合值字段声明为接口类型[…]实际接口可能是
Iesi.Collections.ISet
,System.Collections.ICollection
,System.Collections.IList
,System.Collections.IDictionary
,System.Collections.Generic.ICollection
,System.Collections.Generic.IList
,System.Collections.Generic.IDictionary
,Iesi.Collections.Generic.ISet
或。。。你喜欢什么都行!(其中“任意您喜欢的内容”意味着您必须编写NHibernate.UserType.IUserCollectionType
的实现)
虽然我还没有尝试过,但我相信下面的代码应该很容易自动映射。(但是,不确定私有“超时”成员) 此外,我还为这两个类添加了一个Id属性,因此您应该能够消除“ShouldMap”覆盖
public class Garage
{
public virtual int Id { get; private set; }
public virtual MutexList<Vehicles> Vehicles { get; set; }
public Garage()
{
Vehicles = new MutexList<Vehicles>();
}
}
public class MutexList<T>
{
public virtual int Id { get; private set; }
// Not sure if this will be persisted
private readonly int timeout = 1000;
public virtual IList<T> List { get; set; }
public MutexList<T>()
{
List = new List<T>();
}
}
公共级车库
{
公共虚拟整数Id{get;private set;}
公共虚拟互斥列表车辆{get;set;}
公共车库()
{
车辆=新的互斥列表();
}
}
公共类互斥列表
{
公共虚拟整数Id{get;private set;}
//不确定这是否会持续
私有只读int超时=1000;
公共虚拟IList列表{get;set;}
公共互斥列表()
{
列表=新列表();
}
}
那么,是否可以创建一个常规列表,或者它必须是IList类型?它必须是一个IList
(或任何其他列出的接口类型)。您可以先实例化列表
,但在持久化时,NHibernate将用其内部实现替换它。值得注意的是,始终建议公开接口而不是实现。
public class StoreConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return
type.Name == "Garage"
|| type.Namespace.Contains("Mutex");
}
}
public class Garage
{
public virtual int Id { get; private set; }
public virtual MutexList<Vehicles> Vehicles { get; set; }
public Garage()
{
Vehicles = new MutexList<Vehicles>();
}
}
public class MutexList<T>
{
public virtual int Id { get; private set; }
// Not sure if this will be persisted
private readonly int timeout = 1000;
public virtual IList<T> List { get; set; }
public MutexList<T>()
{
List = new List<T>();
}
}