Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 825355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:14:29+00:00 2026-05-15T03:14:29+00:00

Currently, we use NHibernate to map business objects to database tables. Said business objects

  • 0

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?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:14:30+00:00Added an answer on May 15, 2026 at 3:14 am

    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://nhibernate.info/doc/nh/en/index.html#mapping-declaration-property (Table 5.1. Access Strategies)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're currently migrating our database back-end from Firebird to PostgreSQL. We use NHibernate as
I currently use mysql_real_escape_string to escape a variable when querying the database to prevent
I am currently automatically naming my tables in fluent NHibernate by using AutoMappingOverride's and
I am trying to use NHibernate to persist objects using Guids for their Ids
I am currently trying to map my Java class hierarchy into my database using
Is there currently a way to use the equivalent of the index-many-to-many NHibernate tag
I'm currently trying to use NHibernate.Search, but i need to get score for each
For me the answer is currently: No, I would use iBatis, because NHibernate is
I'm trying to use Hibernate to map a table as a set of DTOs
I currently use Zend_Db to manage my queries $stmt = $db->prepare(INSERT INTO test (ID_Test)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.