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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:43:00+00:00 2026-05-21T23:43:00+00:00

We are using NHibernate on our project and we are hooking into the pre

  • 0

We are using NHibernate on our project and we are hooking into the pre update/insert/delete events to do some auditing.

We want to audit each entity to its own audit table that has the same schema as the source table (maybe with an audit operation such as “Update”, “Insert” etc).

I have looked at the unHAddins that will generate triggers automatically but it seems to drop and recreate the audit tables when you make a change to the main table which we cannot accept, we also need to have some custom logic such that we will only audit the record under certain circumstances of its actual properties (the properties we care about are part of the base class for all of our objects).

To do this simply I figured that I can just extend our existing domain classes and then define a new Nhibernate mapping for those extended classes.

For example:

We have a class Instrument

public class Instrument : BaseObject, IAuditable
    {
        public virtual string Title { get; set; }

        public virtual IList<Control> Controls { get; set; }

        public virtual CouncilRegion Region { get; set; }

        public Type GetAuditType()
        {
            return typeof (InstrumentAudit);
        }

I have defined an interface Iauditable so that we can find out what class is the auditing class for any Iauditable object.

The auditing class is as follows

public class InstrumentAudit : Instrument
{}

It has no additional functionality in it it is basically a hack to let us map it to our audit table in NHibernate.

So this seems like it would work but the problem actually comes in when trying to handle the NHibernate events

public class EventListener: IPreInsertEventListener, IPreUpdateEventListener, IPreDeleteEventListener
{
    private readonly IAuditLogger _logger = new AuditLogger();

    public bool OnPreInsert(PreInsertEvent e)
    {
        Audit(e.Entity as BaseObject, AuditType.Insert);
        return false;
    }
}
 private void Audit(object entity, AuditType auditType)
        {
            if(entity is IAuditable && entity is BaseObject)
            {
                _logger.Log(entity, auditType);
            }
        }

The e.Entity is given to me as an Object.

 public class AuditLogger : IAuditLogger
    {
        public void Log(object entity, AuditType auditType)
        {
            if (entity is IAuditable && entity is BaseObject)
            {
                var auditObject = entity as IAuditable;

                Type type = auditObject.GetAuditType();

                var x = (type) auditObject;

                DataRepository.Instance.Save(x);
            }
        }
    }

Above is the code I would like to work, basically I know that the object is one that should be audited and it is one of my base objects, so I would like to convert it to the subtype of the audit object and pass it to nhibernate to save.

Unfortunately it doesn’t seem like you can cast to a variable it has to be an actual type, is there anyway around this so I can cast or convert the object to its “audit” type without having to put constructors/convertors for each audit type that takes its base type and saves the properties?

  • 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-21T23:43:01+00:00Added an answer on May 21, 2026 at 11:43 pm

    well, the easy answer is, you can’t if your auditObject is no instance of InstrumentAudit or a subclass

    just because there are no additional fields or methods between the Instrument and InstrumentAudit classes, it doesn’t mean you can easily cast from Instrument to InstrumentAudit because Instrument is no subclass of InstrumentAudit

    if you want a way to clone your objects without implementing x converters, have a look @ expression trees: you could setup a dictionary of functions, indexed by the corresponding audit types, that stores “clone” functions … if you find a clone function for the needed type in the dict, use it … if not, create one using expression trees, and store it for later use

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

Sidebar

Related Questions

I'm using NHibernate on a project and I need to do data auditing. I
I am using NHibernate on a new ASP.NET project, and am running into what
We are using NHibernate in our project and need all .hbm.xml files to be
We are using Oracle 10g database, NHibernate, WCF and Silverlight 3.0 in our project
Using NHibernate ICriteria and adding .AddOrder ... I want to sort by a property
I'm using NHibernate 2 and PostgreSQL in my project. SchemaExport class does a great
I'm using nhibernate to store some user settings for an app in a SQL
I'm moving a project from SubSonic to NHibernate and am using Fluent NHibernate to
I'm working on a new project and we're using a pretty nice stack. NHibernate,
We are attepting to profile our NHibernate application using NHProf. We have a series

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.