3

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

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.

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?

Kind regards, Ted

TedOnTheNet
  • 1,082
  • 1
  • 8
  • 23
  • 2
    What I understand here is: You have a single table with three different kinds of objects in it. You want to map it to three entities. Questions: How do you distinguish them in the database? Can you change the database model? Based on what should it be ordered? – Stefan Steinegger Apr 29 '11 at 11:38
  • it can be distinguished using a discriminator column. I can change some parts of the table structure, but not all (since the application is based on a brownfield application and database). Ordering it is done by linenumber. – TedOnTheNet May 01 '11 at 19:25

1 Answers1

6

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
{
  // ...
}

Mapping:

<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>
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193