Nhibernate IsessionFactory的Unity Xml配置 [英] Unity Xml configuration for Nhibernate IsessionFactory

查看:111
本文介绍了Nhibernate IsessionFactory的Unity Xml配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用代码

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

我的服务将属性定义为

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

但是不知道如何使用统一2的XML配置来配置它.

But don't know how to configure this using XML configuration of unity 2.

推荐答案

免责声明,此答案与此处 https://stackoverflow.com/a/29730411/1679310 (评论)

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.

首先,减少全名:

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

  • NHibernate always hydrates many-to-one
  • How to Eager Load Associations without duplication in NHibernate?
  • many-to-many with extra columns nhibernate
  • Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?

这些是我的建议,适用于所有映射.

These would be my suggestions, which apply to all mappings.

通常这应该是类映射

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

这应该是集合映射:

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

这篇关于Nhibernate IsessionFactory的Unity Xml配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆