如何:从同一张表映射(NHibernate)具有不同业务逻辑的多个类? [英] How-to: Mapping (NHibernate) multiple classes with different business logic from the same table?
问题描述
我目前正在使用Brownfield数据库,该数据库包含一个表,该表包含3种不同业务的数据.它与SalesOrders和订单行有关.在旧的应用程序中,我们能够向每个销售订单添加3种类型的订单行:产品,小时费率和文本行. 15年前,这是一种快速而又肮脏的解决方案,可以在Delphi中轻松查询,以将所有行合并到一个datagrid中.每个
I am currently working with a brownfield database which contains a table which holds data for 3 different sorts of business. It has to do with SalesOrders and orderlines. In the old application, we were able to add 3 types of orderlines to each salesorder: products, hourly rates and text lines. 15 years ago, this was a quick and dirty solution to get easy queries in Delphi to get all the lines in one datagrid. Every
现在,我正在尝试使用NHibernate在C#中构建对象模型.由于这3个线型没有真正的业务逻辑连接,因此我制作了3个没有基类的单独实体.但是,我想将这3种类型归为一个列表,以便可以订购它们.
Now I am trying to build the object model in C# using NHibernate. I've made 3 seperate entities without a base class, due to the fact that these 3 line types have no real business logical connection. However, I want to get these 3 types into one list so I can order them.
我考虑过使用继承,每个类使用表,因为表满足要求(没有列具有非null约束).但是,这不是逻辑步骤,因为每种业务类型完全不同(只有共同的地方是userId,描述和备注).也许是一个组成部分?但是如何将属性映射到3个不同的类,而没有基类或除表名之外的任何类型的链接?
I've considered using inheritence, table per class, as the table meets the requirements (no columns with a not-null restraint). This isn't a logical step though, since the business per type is completely different (only things in common are userId, description and remarks). Perhaps a component? but how to map the properties to 3 different classes without a base class or any kind of link except the table name?
我希望你们能理解我写的东西.我还没有真正的代码,我只是在纸上画一些关于如何处理此代码的东西.
I hope you guys understood what I wrote. I have no real code yet, I was just sketching some stuff on paper on how to deal with this code.
这里有人可以帮助我吗?
Anyone here who can help me on my way?
亲切的问候, 特德
推荐答案
您可以在三个实体上放置一个接口,并将其映射为基类,全部映射到同一张表:
You could put an interface on the three entities and map it as a base class, all to the same table:
interface IWhatever
{
// central Id for the whole table
int Id { get; set; }
// may be some more properties
}
class Product : IWhatever
{
// ...
}
class HourlyRate : IWhatever
{
// ...
}
class TextLines : IWhatever
{
// ...
}
映射:
<class name="IWhatever" table="MyBigTable">
<id .../>
<discriminator column="Type"/>
<subclass name="Product" discriminator-value="P">
<!-- ... -->
</subclass>
<subclass name="HourlyRate" discriminator-value="HR">
<!-- ... -->
</subclass>
<subclass name="TextLines" discriminator-value="TL">
<!-- ... -->
</subclass>
</class>
这篇关于如何:从同一张表映射(NHibernate)具有不同业务逻辑的多个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!