views:

242

answers:

2

I'm pondering the use of NHibernate on a project that has a database with some degree of planned denormalization (planned by the DBAs). Reading from one set of tables and mapping one column to one property is not a problem. However when updating I'd have to map one property back to the original column in the original table plus update a few copies of that column in the denormalized tables. Is it possible to do this with NHibernate without using stored procedures?

EDIT: Although I tend to agree with NXC's answer, this question is about how to solve the problem with NHibernate as opposed to solving it in the database.

+1  A: 

Yes, you can register an event listener inheriting from DefaultSaveOrUpdateEventListener, override OnSaveOrUpdate and update the other entities.

Here are some blog posts about event listeners:

Mauricio Scheffer
+1  A: 

Some thoughts on de-normalised data:

  • Use sparingly; excessive use of denormalised data is known to be a source of maintenance and data quality problems down the track. One J2EE project I was involved in had just 4 denormalised data items in 560 tables - two of those items were search tables.

  • Relying on an application to keep de-normalised data in sync is a known anti-pattern. The generally accepted way to maintain de-normalised data in a transactional system is to use database triggers. Using triggers means that an update from any source (not just your application) will be propogated by the database itself. You would be better off using triggers to maintain the denormalised data unless something specific prevents it.

  • Denormalised data leaves elephant traps for maintenance developers (especially if you don't use DB triggers to maintain the data). Make sure each and every piece of denormalised data is documented.

ConcernedOfTunbridgeWells
Good points. Denormalization should not be a concern of the application. However our DBAs consider triggers to be an anti-pattern as well (and I agree to a degree). It's kind of like a "where to put the evil" scenario.
Daniel Auger
I agree on using denormalization seldomly but the second point doesn't apply for a NHibernate-driven app. DB triggers are usually not NHibernate friendly (i.e. they could break the 2nd level cache).Plus, this answer is too generic... it doesn't really answer the question.
Mauricio Scheffer
It absolutely does apply to O/R mapping layers unless you can guarantee that every single database write across the whole life cycle of the database will go through that data access layer.
ConcernedOfTunbridgeWells
With due respect to your DBA's, if they think you should maintain denormalised data in your application code rather than install DB triggers to do it, I'd like to know (a) what they've been smoking and what (b) what's their dealer's mobile number.
ConcernedOfTunbridgeWells