I have a class that I am trying to use the Automapping feature of Fluent on.
The class has a list of objects that are contained in a special class which is contains a List. It is basically written to handle auto locking of the List through various thread.
Basically here is a similar mockup:
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; }
}
}
Here is my auto mapping call:
try
{
var cfg = new StoreConfiguration();
var _factory = Fluently.Configure()
.Database(
MsSqlConfiguration
.MsSql2008
.ConnectionString("Server=192.开发者_开发问答168.0.115;Initial Catalog=EMTRAC3;User ID=EMCollector;Password=9o0(O);")
.ShowSql()
)
.Mappings(m =>
m.AutoMappings.Add(
AutoMap.AssemblyOf<Product>(cfg)
).ExportTo(@"C:\Temp\Fluent")
)
.BuildConfiguration()
.BuildSessionFactory();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
And finally here is the ShouldMap function which I'm not entirely sure why I to call this but without it I don't map anything:
public class StoreConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return
type.Name == "Garage"
|| type.Namespace.Contains("Mutex");
}
}
Can someone tell me I have to do to be able to map the Garage Class with all the Vehicles contained within the List of Vehicles? I'm trying to use the AutoMapping to generate the XML schema for NHibernate on this but I'm not having any luck figuring out how to accomplish the mapping of a list contained within a class in this manner.
You can't map a custom collection class out of the box.
That's very clear in Chapter 6. Collection Mapping:
NHibernate requires that persistent collection-valued fields be declared as an interface type [...] The actual interface might be
Iesi.Collections.ISet
,System.Collections.ICollection
,System.Collections.IList
,System.Collections.IDictionary
,System.Collections.Generic.ICollection<T>
,System.Collections.Generic.IList<T>
,System.Collections.Generic.IDictionary<K, V>
,Iesi.Collections.Generic.ISet<T>
or ... anything you like! (Where "anything you like" means you will have to write an implementation ofNHibernate.UserType.IUserCollectionType
.)
Though I have not tried it, I believe the following code should Automap without difficulty. (However, not sure about the private "timeout" member)
Also, I have added an Id property to both classes, so you should be able to eliminate the "ShouldMap" override.
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>();
}
}
精彩评论