views:

38

answers:

1

Currently, we use NHibernate to map business objects to database tables. Said business objects enforce business rules: The set accessors will throw an exception on the spot if the contract for that property is violated. Also, the properties enforce relationships with other objects (sometimes bidirectional!). Well, whenever NHibernate loads an object from the database (e.g. when ISession.Get(id) is called), the set accessors of the mapped properties are used to put the data into the object.

What's good is that the middle tier of the application enforces business logic. What's bad is that the database does not. Sometimes crap finds its way into the database. If crap is loaded into the application, it bails (throws an exception). Sometimes it clearly should bail because it cannot do anything, but what if it can continue working? E.g., an admin tool that gathers real-time reports runs a high risk of failing unnecessarily instead of allowing an admin to even fix a (potential) problem.

I don't have an example on me right now, but in some instances, letting NHibernate use the "front door" properties that also enforce relationships (especially bidi) leads to bugs.

What are the best solutions?

Currently, I will, on a per-property basis, create a "back door" just for NHibernate:

public virtual int Blah {get {return _Blah;} set {/*enforces BR's*/}}
protected virtual int _Blah {get {return blah;} set {blah = value;}}
private int blah;

I showed the above in C# 2 (no default properties) to demonstrate how this gets us basically 3 layers of, or views, to blah!!! While this certainly works, it does not seem ideal as it requires the BL to provide one (public) interface for the app-at-large, and another (protected) interface for the data access layer.

There is an additional problem: To my knowledge, NHibernate does not give you a way to distinguish between the name of the property in the BL and the name of the property in the entity model (i.e. the name you use when you query, e.g. via HQL--whenever you give NHibernate the name (string) of a property). This becomes a problem when, at first, the BR's for some property Blah are no problem, so you refer to it in your O/R mapping... but then later, you have to add some BR's that do become a problem, so then you have to change your O/R mapping to use a new _Blah property, which breaks all existing queries using "Blah" (common problem with programming against strings).

Has anyone solved these problems?!

+3  A: 

While I found most of your architecture problematic, the usual way to deal with this stuff is having NHibernate use the backing field instead of the setter.

In your example above, you don't need to define an additional protected property. Just use this in the mapping:

<property name="Blah" access="nosetter.lowercase"/>

This is described in the docs, http://nhforge.org/doc/nh/en/index.html#mapping-declaration-property (Table 5.1. Access Strategies)

Diego Mijelshon
This is actually the preferred access strategy for Hibernate: business rules should not be enforced when reading from the database, you should just gets what's in it. For some reason the NHibernate guys don't agree and advocate/recommend the property access strategy.
Fried Hoeben
@Fried that's probably because the setter is not usually where rules are enforced with .NET/NHibernate. In fact, most persistent entities use automatic properties, so the getter/setter bodies are generated.
Diego Mijelshon
Thanks for your comments!@Diego,It appears to me that this solution would cut back on the views to Blah in C# 2 code (just property and field). But in C# 3, it _would_ require me have 2 layers to begin with (property and field)--i.e., Blah cannot be a default property; it must be hand-coded whether there is any business logic to implement yet or not. Do I understand correctly?
apollodude217
@apollodude I don't understand the question. This does not imply an extra layer in your existing code because you are already using non-automatic properties; you are actually *removing* a layer (the protected property).
Diego Mijelshon
I'm sorry that was unclear. Your response answered my question. I said that your solution does require 2, not that that is more or less. To clarify, it is fewer than the C# 2 example above. Your solution, however, means that I cannot start with a default property (via C# 3). And I'm OK with that. I was just trying to make sure I understood you.
apollodude217
Your solution also does away with having to change, e.g., HQL queries when I add business logic. This is the bigger problem with my previous solution.
apollodude217