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 284411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:27:21+00:00 2026-05-12T05:27:21+00:00

I’m using DDD and NHibernate to persist my domain object. In my database every

  • 0

I’m using DDD and NHibernate to persist my domain object. In my database every table that is related to an entity has four extra columns (CreatedBy, UpdatedBy, CreatedDate, UpdatedDate) used for audit tracking. I am using the repository pattern that only allows aggregate roots to be saved. When NHibernate tries to save the entities on the aggregate root, I get a SQL DateTime error because the entities don’t have their audit properties set. Is there a way in NHibernate to set properties on objects just before save?

Here is a little example. I have an Order object which is the aggregate root. I also have OrderNote objects that are children of the order. When I add an OrderNote to the Order and then save the Order an exception is thrown because the CreatedDate/UpdatedDate are set to DateTime.MinValue which will cause a SQL DateTime overflow. Since these audit columns are part of persistence and not related to the problem domain, I don’t want the Order aggregate root to set these properties when the note is added. The audit columns/properties should only be known by the persistence framework and not the domain. I would like to be able to tell NHibernate to set these properties when saving or updating. Is there a way to do this?

  • 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-12T05:27:22+00:00Added an answer on May 12, 2026 at 5:27 am

    Interceptors can do this:

    http://www.nhforge.org/doc/nh/en/index.html#manipulatingdata-interceptors (9.10, link doesn’t always work).

    Cut/paste from the document:

    public class AuditInterceptor : IInterceptor
    {
        ...
    
        public boolean OnSave(object entity,
                              object id,
                              object[] state,
                              string[] propertyNames,
                              IType[] types)
        {
            if ( entity is IAuditable )
            {
                for ( int i=0; i<propertyNames.Length; i++ )
                {
                    if ( "CreateTimestamp" == propertyNames[i] )
                    {
                        state[i] = DateTime.Now;
                        return true;
                    }
                }
            }
            return false;
        }
    
    }
    

    OnFlushDirty() could be used for UpdatedDate.

    You are going to want every entity that has a CreatedDate/UpdatedDate to implement an interface so that the interceptor can check whether it should act (in the provided example it is IAuditable).


    Edit

    Just noticed chapter 11: Interceptors and events. You can inherit from EmptyInterceptor which makes things easier if you only need to override some of the methods.

    I’ve never used Events.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.