views:

34

answers:

1

Hi all

I maintain an application which has many domain entities that draw data from more than one database. The way this normally works is that the entities are loaded from Database A (in which most of their fields are stored). when a property corresponding to data in Database B is called, the entity fires off SQL to Database B to get all the relevant data.

I'm currently using a 'roll-your-own' ORM, which is ugly, but effective (and easy to understand). I've recently started using NHibernate for entities drawn solely from Database A, but I'm wondering how I might use NHibernate for entities drawn from both Databases A and B.

The best way I can think of do this is as follows. I continue to use a NHibernate-based class library for entities in Database A. Those entities which also need data from Database B expose all their data from Database B in a single class accessed via a property. When this property is called, it invokes the appropriate repository, and the object is returned. The class library for accessing Database B would therefore need to be referenced from the class library for accessing Database A.

Does this make any sense, and is there a more established pattern for this situation (which must be fairly common).

Thanks

David

A: 

I don't know how well it maps to your situation, or how mature the NHibernate porting for it is at this point, but you might want to look into Shards.

If it doesn't work for you as-is, it might at least supply some interesting patterns to consider.

EDIT (based on comments):

This indeed doesn't seem to map to your situation, as Shards is about horizontal splitting of data.

If you need to split vertically, you'll probably need to define multiple persistence units. Queries and transactions involving both databases will probably get interesting. I'm afraid I can't really help much with this. This question is definitely related though.

Don Roby
Thanks for the tip, I'll definitely consider it.
David
Looks like sharding is about storing logically equivalent rows into different tables. E.g. people whose surnames start A-M go into one table, and the others go into a second table. In my case, I'm talking about drawing information from databases with completely different schemas, but presenting it to the user as a single thing. Unless I've misunderstood, I don't think Shards can help with this.
David