编程接口,同时用流利的NHibernate映射 [英] Programming to interfaces while mapping with Fluent NHibernate

查看:135
本文介绍了编程接口,同时用流利的NHibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经鞭打就范,并已开始学习功能NHibernate(没有previous NHibernate的经验)。在我的项目,我编程到接口,以减少耦合等也就是说pretty多一切指的是接口而不是具体类型(即时聊天,而不是消息)。这背后的想法是帮助通过能够模拟依赖它更容易测试。

I have been whipped into submission and have started learning Fluent NHibernate (no previous NHibernate experience). In my project, I am programming to interfaces to reduce coupling etc. That means pretty much "everything" refers to the interface instead of the concrete type (IMessage instead of Message). The thought behind this is to help make it more testable by being able to mock dependencies.

不过,(流利)的NHibernate不爱它,当我试图映射到接口,而不是具体的类。这个问题很简单 - 根据维基流利,这是明智的定义我的类ID字段作为实例

However, (Fluent) NHibernate doesn't love it when I try to map to interfaces instead of concrete classes. The issue is simple - according to the Fluent Wiki, it is smart to define the ID field of my class as for instance

int Id { get; private set; }

要得到一个典型自动生成主键。然而,这仅适用于具体类 - 我不能一个界面,在同一行必须在指定的访问级别

to get a typical auto-generated primary key. However, that only works with concrete classes - I can't specify an access level on an interface, where the same line has to be

int Id { get; set; }

和我想这否定使得二传手私人在具体的类(想法是,只有NHibernate的应该永远设置由DB分配ID)。

and I guess that negates making the setter private in the concrete class (the idea being that only NHibernate should ever set the ID as assigned by the DB).

现在,我想我只会让公众制定者,尽量避免写它的诱惑。但任何人都不会有什么会是正确的想法,最佳实践方法来建立一个适当的只有NHibernate的可以写入,同时还只面向接口编程的主键字段?

For now, I guess I will just make the setter public and try to avoid the temptation of writing to it.. But does anyone have an idea of what would be the "proper", best-practice way to create a proper primary-key field that only NHibernate can write to while still only programming to interfaces?

更新时间:

这是我从mookid和詹姆斯·格雷戈里下面的两个答案后明白了,我可能是在错误的轨道上 - 不应该有,我有每个实体的接口,因为我现在有一个原因。这一切都很好。我想那么我的问题是 - 有没有理由对任何实体的接口程序100%?如果有,甚至一个单一的情况下,这可能是合理的,是有可能与(流利)NHibernate的做到这一点?

From what I understand after the two answers below from mookid and James Gregory, I may well be on the wrong track - there shouldn't be a reason for me to have an interface per entity as I have now. That's all well and good. I guess my question then becomes - is there no reason to program 100% against an interface for any entities? And if there is even a single situation where this could be justified, is it possible to do this with (Fluent) NHibernate?

我问,因为我不知道,更不要是至关重要的。感谢您的答复。 :)

I ask because I don't know, not to be critical. Thanks for the responses. :)

推荐答案

更新:不通过流畅的界面流畅,NHibernate的支持提供了使用union-子类。你必须使用一个普通的HBM映射文件,并添加它。

UPDATE: using union-subclass is not supported via the fluent interface fluent-nhibernate provides. You'll have to use a regular hbm mapping file and add it.

我也我试图用流利的NHibernate的做到这一点。我不认为这应该是一个问题的映射接口。你想用一个继承策略,特别是<一个href=\"http://docs.jboss.org/hibernate/stable/core/reference/en/html/inheritance.html\">table-per-concrete-class战略。

I too I'm trying do this with fluent NHibernate. I don't think it should be a problem mapping interfaces. You want to use an inheritance strategy, specifically the table-per-concrete-class strategy.

从本质上讲,创建基类映射定义(在这种情况下,您的接口),并指定应如何处理的NHibernate通过使用工会子实施者。

Essentially, you create a mapping definition for the base class (in this case your interface) and specify how to NHibernate should deal with implementers by using union-subclass.

因此​​,举例来说,这应该让你做出多态关联:

So, for example, this should allow you to make polymorphic associations:

<class name="IAccountManager"
                abstract="true"
                table="IAccountManager">

        <id name="Id">
                <generator class="hilo"/>
        </id>

        <union-subclass
                table="DefaultAccountManager"
                name="DefaultAccountManager">
                <property name="FirstName"/>
        </union-subclass>

        <union-subclass
                table="AnotherAccountManagerImplementation"
                name="AnotherAccountManagerImplementation">
                <property name="FirstName"/>
        </union-subclass>
        ...
</class>

请注意的Id是如何为所有具体实施者是相同的。 NHibernate的需要这一点。此外,IAccountManager表实际上不存在

Note how the Id is the same for all concrete implementers. NHibernate required this. Also, IAccountManager table doesn't actually exist.

您也可以尝试,并充分利用NHibernate的隐式多态(记录下表每具体类策略) - 但它有局限性吨的。

You can also try and leverage NHibernate's Implicit Polymorphism (documented below the table-per-concrete-class strategy) - but it has tons of limitations.

这篇关于编程接口,同时用流利的NHibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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