简体   繁体   English

实体类中的流利NHibernate映射类

[英]Fluent NHibernate mapping class within the entity class

I'm studying Fluent NHibernate now, and have a question about mapping. 我现在正在研究Fluent NHibernate,并且对映射有疑问。 It's not an issue, but a best practice question. 这不是问题,而是最佳实践问题。

I know that with Fluent NHibernate there is a new fluent mapping, and it requires a new Class for mapping fields that will be used by the Entity Class. 我知道Fluent NHibernate有一个新的Fluent映射,它需要一个新的Class来映射将由Entity Class使用的字段。 I was wondering, if the Mapping Class is directly linked to the Entity Class (It will map exacly for the entity class), do best practices dictate that they can't be joined within the same .cs file? 我想知道,如果映射类直接链接到实体类(它将为实体类进行映射),那么最佳实践是否规定它们不能在同一.cs文件中加入? Please note that there will be no nesting here. 请注意,这里不会嵌套。

Ie: There are Product and a ProductMap classes, both for a Product table on my database, so I'd place both classes within the same Product.cs, like the following: 即:我的数据库中有一个Product表和一个ProductMap类,它们都用于Product表,因此我将这两个类放在同一Product.cs中,如下所示:

namespace Business.Entity
{
    public class Product
    {
        ...
    }

    public class ProductMap : ClassMap<Product>
    {
        ...
    }
}

If the classes shouldn't be inside the same file, would you care to explain why, and maybe with real examples? 如果这些类不应该在同一个文件中,那么您是否愿意解释原因,也许还带有实际示例?

Thanks in advance! 提前致谢!

When creating the SessionFactory instance, you will pass a class that tells which assembly has the mapping definitions. 创建SessionFactory实例时,您将传递一个类,该类告诉哪个程序集具有映射定义。 Then, using reflection, it'll iterate through all the classes on this assembly that inherit from ClassMap<T> . 然后,使用反射将遍历此程序集上所有从ClassMap<T>继承的类。

That said, for a faster initialisation, it's better to have this assembly as light as possible, and it means that it would be better to have an assembly that would hold only the mappings and not the classes definitions. 就是说,为了更快地初始化,最好使该程序集尽可能轻,这意味着最好只包含映射而不包含类定义的程序集。

AFAIK this is the only difference. AFAIK这是唯一的区别。 Any feedback will be appreciated. 任何反馈将不胜感激。

You should design your entities persistant ignorant as much as possible. 您应该尽可能将实体持久性设计为无知的。

That means you shouldn't make Product derive from ClassMap<Product> . 这意味着您不应该使ProductClassMap<Product>派生。 But it also means that the ProductMap shouldn't even be in the same project as your entity. 但这也意味着ProductMap甚至不应与您的实体位于同一项目中。

Typically, you would have a DAL project that contains the mapping and a Domain / Business project that contains the entity 通常,您将有一个包含映射的DAL项目和一个包含实体的Domain / Business项目

You can keep both classes in different files, even in different namespace. 您可以将两个类保留在不同的文件中,甚至可以保留在不同的名称空间中。 but if you are beginner then you can keep entity class in different file and map class in different file within same namespace. 但是,如果您是初学者,则可以将实体类保存在不同的文件中,并将映射类保存在相同的命名空间中的其他文件中。

/* Product.cs */
namespace Business.Entity
{
    public class Product
   {
    ...
   }
}

/* ProductMap.cs */
namespace Business.Entity
{
    public class ProductMap : ClassMap<Product>
    {
    ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM