开发者

NHibernate and Modular Code

开发者 https://www.devze.com 2022-12-14 06:34 出处:网络
We\'re developing an application using Nhibernate as the data access layer. One of the things I\'m struggling with is finding a way to map 2 objects to the same table.

We're developing an application using Nhibernate as the data access layer.

One of the things I'm struggling with is finding a way to map 2 objects to the same table.

We have an object which is suited to data entry, and another which is used in more of a batch process.

The table contains all the columns for the data entry and some additional info for the batch processes.

When it's in a batch process I don't want to load all the data just a subset, but I want to be able to update the values in the table.

Does nhibernate support multiple objects pointed at the same table? and what is the thing that allows this?

I tried it a while ago and I remember that if you do a query for one of the objects it loads double the amount but i'm not so sure I didn't miss something.

e.g.

10 data entry objects + 10 batch objects

So 20 object instead of 10.

Can anyone shed any light on this?

I should clarify that these objects are 2 different objects which in my mind should not be polymorphic in behaviour. However they do point at the same database record, it's more that the record has a dual purpose within the applicati开发者_StackOverflowon and for sake of logical partitioning they should be kept separate. (A change to one domain object should not blow up numerous screens in other modules etc).

Thanks Pete


An easy way to map multiple objects to the same table is by using a discriminator column. Add an extra column to the table and have it contain a value declaring it as type "Data Entry" or "Batch Process".

You'd create two objects - one for Data Entry and Batch Process. I'm not entirely sure how you enact that in regular NHibernate XML mapping - I use Castle ActiveRecord for annotating, so you'd mark up your objects like so:

[ActiveRecord("[Big Honking Table]",
    DiscriminatorColumn = "Type",
    DiscriminatorType = "String",
    DiscriminatorValue = "Data Entry")]
public class Data Entry : ActiveRecordBase
{
   //Your stuff here!
}

[ActiveRecord("[Big Honking Table]",
    DiscriminatorColumn = "Type",
    DiscriminatorType = "String",
    DiscriminatorValue = "Batch Process")]
public class Batch Process : ActiveRecordBase
{
   //Also your stuff!
}

Here's the way to do it with NHibernate + Castle ActiveRecord: http://www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html Note that they use a parent object - I don't think that's necessary but I haven't implemented a discriminator column exactly the way you're describing, so it might be.

And here's the mapping in XML: https://www.hibernate.org/hib_docs/nhibernate/html/inheritance.html

You can also, through the mapping, let NHibernate know which columns to load / update - if you end up just making one big object.


I suppose you just might be overengineering it just a little bit:

  • If you worry about performance, that's premature optimization (besides, retrieving less columns is not much faster, as for saving you can enable dynamic updates to only update columns that changed).
  • If you trying to protect the programmer from himself by locking down his choices, you complicating your design for not so noble a cause.

In short, based on my 10 yrs+ of experience and my somewhat limited understanding of your problem I recommend you think again about doing what you wanna do.

0

精彩评论

暂无评论...
验证码 换一张
取 消