English 中文(简体)
我如何在不制造重复性财产的情况下绘制新赫伯特的重复一栏地图
原标题:How do I map repeating columns in NHibernate without creating duplicate properties
  • 时间:2010-03-29 00:27:20
  •  标签:
  • nhibernate

鉴于有多个重复栏用于审计和版面的数据库,使用NHibernate的最佳方式是不必重复本领域每一类的栏目?

数据库的每个表格都重复了这9个栏目,名称和类型是相同的,我不想在域模型中复制。

我阅读了该书,我看到了关于遗产规划的章节,但我看不出如何在这一设想中发挥作用。 这似乎是一种共同的设想,因为几乎每个数据库都有四个共同审计栏(CreatedBy, CreatDate, UpdatedBy, UpdateDate)。 这个数据库没有变化,只是引入每个表格共有的另外五个栏目。

最佳回答

可以通过在绘图档案中使用构成部分来做到这一点。

基本想法是设立一个类别,以掌握共同财产,并在你的模式中从每个实体那里查阅。

然后,在您的地图档案中添加提及此类财产的内容。

<component name="RecordMetadata" class="RecordMetadata" insert="true" update="true">
    <property name="UpdatedBy" />
    <property name="UpdatedDate" />
    <property name="CreatedBy" />
    <property name="CreatedDate" />
</component>
问题回答

http://fluentnhibernate.org/“rel=“nofollow noreferer”>Fluent NHibernate,以创建你的绘图档案。 这使你能够利用你的绘图档案继承遗产。 例如:

public class AuditableClassMap<T> : ClassMap<T> where T : IAuditable
{
    public AuditableClassMap()
    {
        Map(x => x.CreatedBy);
        Map(x => x.CreatedDate, "CreatedDt");
        Map(x => x.RevisedBy);
        Map(x => x.RevisedDate, "RevisedDt");
    }
}

public class CompanyMap : AuditableClassMap<Company>
{
    // mapping for Company
}

借助编号4的代号,你应当能够撰写一份单一的代号文件,该代号为:.hbm.xml文档,其所有类别都有界定。 我设想如下。 首先,在<代码>.tt的延伸中编造一个文档,并在以下代码中:

<#@ template language="C#v3.5" #>
<#@ output extension="hbm.xml" #>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyNameSpace">
<#
System.Collections.Generic.Dictionary<string, string> classes = new System.Collections.Generic.Dictionary<string, string>();
classes.add("RootNameSpace.SubNameSpace.MyClass1", "Table1");

foreach(string className in classes.keys)
{
#>
  <class name="<#=className#>, AssemblyName" table="<#=classes[className]#>">
    <id name="ID" column="EntityID" type="Int32">
      <generator class="native" />
    </id>
    <property name="Property1" />
    <property name="Property2" />
  </class>
<#
}
#>
</hibernate-mapping>

最后一个步骤是将产出文件编成“建设行动”,以吸收资源,而且你应当做得好。

You can read more about t4 code generation here: http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

http://code.google.com/p/codeconform/“rel=“nofollow noretinger”>ConfORM。

在许多情况下,它将做你的工作all。 如果是这样的话,就很容易界定你们的公约和凌驾一切。





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签