问题
I resolved session factory using code as
UnityContainer.RegisterInstance(typeof(ISessionFactory),
new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory());
My service which defines property as
public class myService
{
[Dependency]
public ISessionFactory SessionFactory{get;set;}
}
But don't know how to configure this using XML configuration of unity 2.
回答1:
Disclaimer, this answer is more related to the content discussed in here https://stackoverflow.com/a/29730411/1679310 (comments)
Let me provide you with more details, related to your mapping.
Firstly, reduce full names:
// here we have namespace, not needed below for classes, if is the same
<hibernate-mapping ... namespace="NhibernateTesting.Models" >
The original class mapping is commented, my approach is replacing it:
// <class name="NhibernateTesting.Models.ProductList" table="ProductList" lazy="false">
<class name="ProductList" table="ProductList"
lazy="true" // always use LAZY
batch-size="25" // optimization for loading - avoid 1 + N
>
//<id name="Id">
// <generator class="native" />
//</id>
// more readable version
<id name="Id" generator="native" />
// these are OK
<property name="Name" />
<property name="Owner" />
// collection should be ALWAYS lazy
// <bag name="Viewers" table="ProductListViewer" lazy="false">
// and also, optimization as batch size is needed
<bag name="Viewers" table="ProductListViewer"
lazy="true" // LAZY is must,
batch-size="25" cool optimization to avoid 1 + N
>
<key column="ProductListId" />
<element column="Username" type="System.String"/>
</bag>
// I would never use MANY-TO-MANY
// <bag name="Products" table="ProductListProductXRef" lazy="false" cascade="all" fetch="select">
<bag name="Products" table="ProductListProductXRef"
inverse="true" // in many to many only one side could be inverse
lazy="true"
cascade="none" // cascade here means, delete PRODUCTS
// not the pairing table, so I would expect that we need none
fetch="select">
<key column="ProductListId" />
// I would avoid many to many if possible
<many-to-many column="ProductId" class="NhibernateTesting.Models.Product" />
...
Check these links for more details.
Laziness
- NHibernate When to Use lazy loading?
Batch fetching
- NHibernate always hydrates many-to-one
- How to Eager Load Associations without duplication in NHibernate?
Avoid many to many
- many-to-many with extra columns nhibernate
- Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?
When to use cascading
- nhibernate - Create child by updating parent, or create explicitly?
These would be my suggestions, which apply to all mappings.
In general this should be the class mapping
<class
name="ProductList"
table="ProductList"
lazy="true"
batch-size="25"
>
In case we need versioning:
<class
name="ProductList"
table="ProductList"
lazy="true"
batch-size="25"
optimistic-lock="version"
dynamic-update="true"
>
<version name="Timestamp" generated="always" unsaved-value="null" type="BinaryBlob">
<column name="RowVersion" not-null="false" sql-type="timestamp"/>
</version>
This should be the collection mapping:
<bag name="Items"
lazy="true"
inverse="true"
batch-size="25"
cascade="all-delete-orphan">
<key column="Item_ID" />
<one-to-many class="SomeItemClass"/>
</bag>
来源:https://stackoverflow.com/questions/29742478/unity-xml-configuration-for-nhibernate-isessionfactory