C# Nhibernate IsessionFactory的统一Xml配置

C# Nhibernate IsessionFactory的统一Xml配置,c#,nhibernate,unity-container,xml-configuration,C#,Nhibernate,Unity Container,Xml Configuration,我使用代码解析了会话工厂 UnityContainer.RegisterInstance(typeof(ISessionFactory), new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory()); 我的服务,它将属性定义为 public class myService { [Dependency] public ISessionFactory SessionFacto

我使用代码解析了会话工厂

UnityContainer.RegisterInstance(typeof(ISessionFactory),
                new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory());
我的服务,它将属性定义为

public class myService
{
   [Dependency]
   public ISessionFactory SessionFactory{get;set;}
}

但是我不知道如何使用unity 2的XML配置来配置它。

免责声明,这个答案与这里讨论的内容(注释)更相关

让我为您提供更多有关映射的详细信息

首先,减少全名:

// here we have namespace, not needed below for classes, if is the same
<hibernate-mapping  ... namespace="NhibernateTesting.Models" >
//这里有名称空间,下面的类不需要名称空间,如果名称空间相同
对原始类映射进行了注释,我的方法是替换它:

// <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" />
 ...
//
//
//  
//
//更具可读性的版本
//这些没问题
//集合应该总是懒惰的
// 
//此外,还需要根据批量大小进行优化
//我永远不会用多对多的方式
// 
//如果可能的话,我会避免多对多
...
查看这些链接以了解更多详细信息

懒惰
批量取数
避免多对多
何时使用级联
这些是我的建议,适用于所有映射

通常,这应该是类映射

<class 
  name="ProductList" 
  table="ProductList" 
  lazy="true"
  batch-size="25"
>

如果我们需要版本控制:

<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>

这应该是集合映射:

<bag name="Items" 
   lazy="true" 
   inverse="true" 
   batch-size="25" 
   cascade="all-delete-orphan">
  <key column="Item_ID" />
  <one-to-many class="SomeItemClass"/>
</bag>