NHibernate; basics of Bi-directional 1:m, m:m and List/IList
My understanding of List vs IList and other collections is quite limited, so I need to ask:
NHibernate documentation says:
Please note that NHibernate does not support bidirectional one-to-many associations with an indexed collection (list, map or array) as the "many" end, you have to use a set or bag mapping.
D开发者_StackOverflow中文版oes the implementation of the class below respect this limitation? In other words; Does it suffice to declare the property as an IList while still being initialized as a concrete List? Or I need to use a different type of collection?
And can I use a specific implementation of any container, or does it have to be an interface?
Example is from the FluentNHibernate tutorial, so I assume it would be ok, but I would like to understand it more in-depth than just to accept it on that basis.
public class Store
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
public virtual IList<Employee> Staff { get; set; }
public Store()
{
Products = new List<Product>();
Staff = new List<Employee>();
}
public virtual void AddProduct(Product product)
{
product.StoresStockedIn.Add(this);
Products.Add(product);
}
public virtual void AddEmployee(Employee employee)
{
employee.Store = this;
Staff.Add(employee);
}
}
As long as you map it as bag
, that's fine.
Keep in mind that NHibernate will replace your List
with a PersistentGenericBag
when persisting/loading, so always program to the interface.
精彩评论