【问题标题】:Unity Xml configuration for Nhibernate IsessionFactoryNhibernate IsessionFactory 的 Unity Xml 配置
【发布时间】:2015-06-26 20:37:26
【问题描述】:

我使用代码解析会话工厂

UnityContainer.RegisterInstance(typeof(ISessionFactory),
                new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory());

我的服务将属性定义为

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

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

【问题讨论】:

    标签: c# nhibernate unity-container xml-configuration


    【解决方案1】:

    免责声明,此答案与此处https://stackoverflow.com/a/29730411/1679310(cmets)中讨论的内容更相关

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

    首先,减少全名:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多