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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:05:06+00:00 2026-06-01T09:05:06+00:00

I am creating an ASP.NET MVC3 application using NHIBERNATE. I have a base class

  • 0

I am creating an ASP.NET MVC3 application using NHIBERNATE. I have a base class Entity that is used to capture some audit information like CreatedBy, CreatedOn, UpdatedBy, UpdatedOn, etc. These properties should get populated automagically whenever a derived class is created and persisted/updated.

As the Entity type and all other types are defined in the DOMAIN Assembly, What would be the best way to percolate USER information from Web project to DOMAIN to populate the information in Entity base class

EDIT

Further,

  1. Currently, i define all properties as public virtual, so how do I implement them so that the dates/userId are created on the fly at the time of persistence.
  2. I want Base class properties to persist in each table for derived concrete classes (table per concrete class). How do i specify it using FLUENT NHIBERNATE (I am not using Automapping)
  • 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-06-01T09:05:07+00:00Added an answer on June 1, 2026 at 9:05 am

    Use an Interceptor when you open the session:

    sessionFactory.OpenSession(new AuditInterceptor());
    

    File: AuditInterceptor.cs

    using System;
    using NHibernate;
    using NHibernate.Type;
    using System.Threading;
    using System.Security;
    
    namespace bla
    {
        /// <summary>
        /// NHibernate Interceptor that stores audit information (idlogin and datetime.now) on every save and update
        /// </summary>
        public class AuditInterceptor : EmptyInterceptor
        {
    
            private int updates;
            private int creates;
            private int loads;
            private ISession session;
    
            public override void OnDelete(object entity,
                                          object id,
                                          object[] state,
                                          string[] propertyNames,
                                          IType[] types)
            {
                // do nothing
            }
    
            public override bool OnFlushDirty(object obj, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
            {
                bool found = false;
                if (obj is Entity)
                {
                    updates++;
                    for (int i = 0; i < propertyNames.Length; i++)
                    {
                        if ("dtlastchanged".Equals(propertyNames[i].ToLower()))
                        {
                            currentState[i] = DateTime.Now;
                            found = true;
                        }
                        if ("lastchangedby".Equals(propertyNames[i].ToLower()))
                        {
                            currentState[i] = this.session.Get<Login>(IdLogin, LockMode.None);
                            found = true;
                        }
                    }
                }
                return found;
            }
    
            public override bool OnLoad(object obj, object id, object[] state, string[] propertyNames, IType[] types)
            {
                if (obj is Entity)
                {
                    loads++;
                }
                return false;
            }
    
            public override bool OnSave(object entity,
                                        object id,
            object[] state,
            string[] propertyNames,
            IType[] types)
            {
                bool found = false;
                if (entity is Entity)
                {
                    creates++;
                    for (int i = 0; i < propertyNames.Length; i++)
                    {
                        if ("dtlastchanged".Equals(propertyNames[i].ToLower()))
                        {
                            state[i] = DateTime.Now;
                            found = true;
                        }
                        if ("lastchangedby".Equals(propertyNames[i].ToLower()) && !(entity is Login && (entity as Login).IsInVerification))
                        {
                            state[i] = this.session.Get<Login>(IdLogin, LockMode.None);
                            found = true;
                        }
                    }
                }
                return found;
            }
    
            public override void AfterTransactionCompletion(ITransaction tx)
            {
                //if (tx.WasCommitted)
                //{
                //    System.Console.WriteLine("Creations: " + creates + ", Updates: " + updates, "Loads: " + loads);
                //}
                updates = 0;
                creates = 0;
                loads = 0;
            }
    
            public override void SetSession(ISession session)
            {
                this.session = session;
                base.SetSession(session);
            }
    
            protected long IdLogin
            {
                get
                {
                    return (Thread.CurrentPrincipal.Identity as CustomIdentity).IdLogin; // or something else that holds the id of the current logged in user
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an ASP.NET MVC application that has postcode lookup functionality. I capture
I am creating a new web application using ASP.NET MVC3 Razor and HTML 5.
I have an ASP.NET MVC3 application in which I am creating multiple areas, is
Currently, I am creating an application using ASP.NET MVC3 and MySQL and when I
I'm looking at creating my first ASP.NET MVC application using MVC3. The project template
I have an ASP.NET MVC 3 application. I'm using AsyncController and creating new threads.
I have an employee class in model for ASP.NET MVC3. There is a field
I have an ASP.NET MVC3 in C# and Razor. The architecture of the application
I'm working on MVC3 and currently creating a Forum application in asp.net. Forum allows
I'm working on an ASP.NET MVC app (using MVC3 RC2). Say I have 2

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.