开发者

Best practice: should I use FK on DB using nHibernate/FluentNhibernate?

开发者 https://www.devze.com 2023-01-30 02:42 出处:网络
So far I a开发者_JAVA技巧lways enforce my DB with FK relationship. Things changed yesterday while mapping some classes with FluentNhibernate. My mapping didn\'t work and I discovered that\'s the issue

So far I a开发者_JAVA技巧lways enforce my DB with FK relationship. Things changed yesterday while mapping some classes with FluentNhibernate. My mapping didn't work and I discovered that's the issue was because of the order FN create the query.

Now a question arise: should I keep enforcing data with FK or it's better to avoid it since I focus on domain classes instead of sql queries?

Thanks


To my knowledge, it will be far better to keep your database consistent, cause you may not be the only one who works on this DB in future, and maybe someone else have access to the DB and do sth that could corrupt your data consistency and as a result your application also doesn't behavior in the way you expect because of assummed conditions that no longer hold.


Letting Fluent/NH create your database during development is fine, but when it goes into production you really should check all the foreign keys, index's, etc etc and then only do scripted changes there on after.


Keep your database consistent, maintain referential integrity.

If a tool you are using breaks as a result there is bound to be a workaround. However if you lose referential integrity to use nhibernate - what happens if you decide to use a different ORM? You will have a dodgy database and who's to say that the next ORM in line will like that?

Its like a separation-of-concerns question, each chunk of your application should be designed to be robust enough to survive if another chunk is changed or removed - so don't change good database practice simply to make a product that is layered above it play nicely.


Using a domain-driven approach , or model oriented approach where the DB is merely seen as an 'implementation-detail', does not mean that you should ignore the integrity of your data. I see no reason why you should drop foreign-key (and other) constraints from your database.

The database is more then just a storage for your data. It's task is also to guard the integrity of it.

It is perfectly possible to combine the 2 worlds (domain driven and relational database) with NHibernate. Make sure that the 2 areas focus on what they're best at. And, the database is best at storing data and making sure that the data remains valid / integer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号