views:

183

answers:

2

Hi,

I've got a parent and child object. Depending on a value in the parent object changes the table for the child object. So for example if the parent object had a reference "01" then it will look in the following table "Child01" whereas if the reference was "02" then it would look in the table "Child02". All the child tables are the same as in number of columns/names/etc.

My question is that how can I tell Fluent Nhibernate or nhibernate which table to look at as each parent object is unique and can reference a number of different child tables?

I've looked at the IClassConvention in Fluent but this seems to only be called when the session is created rather than each time an object is created.

A: 

NHibernate is intended to be an object relational mappers. It sounds like you're doing more of a scripting style and hoping to map your data instead of working in an OOP manner.

It sounds like you have the makings of an class hierarchy though. What it sounds like you're trying to create in your code (and then map accordingly) is a hierarchy of different kinds of children:

BaseChild

--> SmartChild

--> DumbChild

Each child is either smart or dumb, but since they all have a FirstName, LastName, Age, etc, they all are instances of the BaseChild class which defines these. The only differences might be that the SmartChild has an IQ and the DumbChild has a FavoriteFootballTeam (this is just an example, no offense to anyone of course ;).

NHibernate will let you map this sort of relationship in many ways. There could be 1 table that encompasses all classes or (what it sounds like you want in your case), one table per class.

Did I understand the issue/what you're looking for?

statichippo
You kinda got what I'm after. The situation I'm in that the parent class which in this case is an "Account" has a child list of "Account Information". The way in which the db is structured is that an account has a year associated with it and this refers to the Account Information db table so for 2001 the table is AcctInfo01, 2002 is AcctInfo02 and so on. I dont want multiple list classes in my parent (Account) object I'd rather just have one property called AccountInfo which is a list from the relevant table. Is this even possible?
Simon G
I don't know that there's a way to do this, but I'm definitely not an NHibernate expert.
statichippo
+1  A: 

I found only two methods to do this.

  1. Close and recreate the nhibernate session every time another dynamic table needs to be looked at. On creating the session use IClassConvention to dynamically calculate the name based on user data. I found this very intensive as its a large database and a costly operation to create the session every time.
  2. Use POCO object for these tables with custom data access.

As statichippo stated I could use a basechild object and have multiple child object. Due to the database size and the number of dynamic table this wasn't really a valid option.

Neither of my two solutions I was particularly happy with but the POCO's seemed the best way for my problem.

Simon G