Andrew Whitaker
Andrew Whitaker

Reputation: 126042

How to handle default data in an application using NHibernate

I'm working on an application that uses SQL Server and NHibernate. We have the concept of default data (complex entities) that needs to be created for each new entity. This data can be changed on a per-user basis. However, we're struggling with the best way to create this data.

For example, lets say my application has a Store entity which has several default Products that I want to create when a new Store gets created. Anything about aProduct can be modified by managers of each Store.

As I see it, there are two main options:

  1. Keep the default data in code and write it to the database once the new entity is created.
  2. Keep the default data in the database and move it over with a stored procedure/raw SQL when the entity is created.

Instinctively, I lean toward option two, since databases are great at moving and manipulating sets of data, and option one would require a ton of messy code that could get out of hand.

However, writing a stored procedure or raw SQL presents its own issues:

I found this article by Ayende Rahien which outlines how to perform a bulk delete. I am thinking that doing something similar for inserting default data would be fine. I also found an nhibernate users groups post (called "Schema export and default data"--SO won't let me post two links) that describes a similar situation, but it doesn't seem like there's a consensus on what the right solution is (although Ayende does offer some feedback and suggests that the data live in the database).

After writing this, I'm leaning even more toward using a stored procedure, I'm just worried about possible pitfalls of mixing two database access strategies (directly calling SProcs and using an ORM).

Any feedback is appreciated!

Edit: Removed "immutable" language. I'm specifically talking about default data that can change so I think this term was incorrect/confusing here.

Upvotes: 3

Views: 376

Answers (1)

Antoine Jaussoin
Antoine Jaussoin

Reputation: 5172

I would create a default data service that creates those data in code, and use a factory to create your store and use the default data service to generate the default entities. Using a Stored Procedure definitely defeats the point of having an ORM.

Upvotes: 1

Related Questions