NHibernate.Mapping.ByCode.Conformist.ClassMapping和FluentNHibernate.Mapping.ClassMap有什么区别? [英] What is the difference between NHibernate.Mapping.ByCode.Conformist.ClassMapping and FluentNHibernate.Mapping.ClassMap?

查看:303
本文介绍了NHibernate.Mapping.ByCode.Conformist.ClassMapping和FluentNHibernate.Mapping.ClassMap有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习NHibernate,我学习的类映射是用XML完成的。我明白Fluent NHibernate是一种强类型的XML映射风格的替代品。事实上,下面是 fluent-nhibernate 标记的描述:
$ b


流利的NHibernate让你在强类型的
C#代码中编写NHibernate映射。这允许轻松重构,提高可读性和
更简洁的代码。

然后,我以后使用 NHibernate Mapping Generator 从我现有的数据库创建映射和域类,它生成的映射代码如下:

 使用NHibernate.Mapping.ByCode.Conformist; 
使用NHibernate.Mapping.ByCode;

命名空间MyNamespace.Infrastructure.Mappings
{
public class MyItemMapping:ClassMapping< MyItem>
{
public MyItemMapping()
{
Table(MyItems);
Schema(master);
懒惰(true);
Id(x => x.ID,map => map.Generator(Generators.Assigned));
Property(x => x.Status,map => map.NotNullable(true));
Property(x => x.DueDate,map => map.NotNullable(true));
Property(x => x.NextReminderDate);
Property(x => x.DatePaid);
Property(x => x.Notes);





你看,它使用一个 NHibernate.Mapping.ByCode.Conformist.ClassMapping< T> 类。是什么赋予了? 如果NHibernate实际上 有它自己的强类型,非XML映射能力,那么为什么我需要流利的NHibernate呢?



我注意到 NHibernate.Mapping.ByCode.Conformist.ClassMapping< T> FluentNHibernate.Mapping.ClassMap< T> 。例如,前者不支持 References ,例如引用(x => x.BillingItemID); ,通过外键关联实体。也许还有另外一种方法。

解决方案

FluentNHibernate在NHibernate之前就有了MappingByCode,现在它的确如此,FluentNHibernate已经过时了,它也比Nhibernate自己的MappingByCode效率低,因为它在启动时会生成正常的XML映射文件并在内部使用它们。



http://notherdev.blogspot.co.uk/2012/02/nhibernates-mapping-by-code-summary.html <不过我不管用NHibernate的版本。我觉得NHibernate的版本实际上比FluentNhibernate支持的更多,相当于 Reference 只是一个关系的反面。如果父项映射为 OneToMany(),那么与Fluent的 Reference 等价的子对象映射将是多对一()。无论如何,我认为是这样。


I'm learning about NHibernate, where the class mapping, I learned, is done with XML. I understand that Fluent NHibernate came about as a strongly-typed replacement for the XML-style of mapping. Indeed, here is the fluent-nhibernate tag description:

Fluent NHibernate lets you write NHibernate mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.

Then later I was using NHibernate Mapping Generator to create mappings and domain classes from my existing database, and it generated mapping code like this:

using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Mapping.ByCode;

namespace MyNamespace.Infrastructure.Mappings
{
    public class MyItemMapping  : ClassMapping<MyItem> 
    {
        public MyItemMapping()
        {
            Table("MyItems");
            Schema("master");
            Lazy(true);
            Id(x => x.ID, map => map.Generator(Generators.Assigned));
            Property(x => x.Status, map => map.NotNullable(true));
            Property(x => x.DueDate, map => map.NotNullable(true));
            Property(x => x.NextReminderDate);
            Property(x => x.DatePaid);
            Property(x => x.Notes);
        }
    }
}

Lo and behold, it's using a NHibernate.Mapping.ByCode.Conformist.ClassMapping<T> class. What gives? If NHibernate in fact does have it's own strongly-typed, non-XML mapping capabilities, then why do I need Fluent NHibernate?

I've noticed some differences between NHibernate.Mapping.ByCode.Conformist.ClassMapping<T> and FluentNHibernate.Mapping.ClassMap<T>. For example, the former doesn't support References, e.g. References(x => x.BillingItemID);, to relate entities via the foreign key. Maybe there's another way of doing that.

解决方案

FluentNHibernate was around before NHibernate had MappingByCode, now that it does, FluentNHibernate is obsolete, it's also less efficient than Nhibernate's own MappingByCode because it generates normal XML mapping files at startup and uses them internally.

The only downside to NHibernate MappingByCode is that there isn't much documentation for it, the best I've found is here:

http://notherdev.blogspot.co.uk/2012/02/nhibernates-mapping-by-code-summary.html

But I would use NHibernate's version regardless. I'm under the impression that NHibernate's version actually supports more than FluentNhibernate does as well, the equivalent of that Reference would just be the opposite side a relationship, e.g. if the parent is mapped as OneToMany() then the equivalent child side map to Fluent's Reference would be a ManyToOne(). I think that's the case anyway.

这篇关于NHibernate.Mapping.ByCode.Conformist.ClassMapping和FluentNHibernate.Mapping.ClassMap有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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