0

I have a custom setter for an object property that I want to add some custom logging to, such that I can look at the logs and see when that value is changed. However, it seems that whenever NHibernate reads that value from the Database and populates the object, the logging gets triggered. Is there a way to programatically determine that the change is happening because NHibernate is populating the object?

public virtual string URL
        {
            get
            {
                return pURL;
            }
            set
            {
                if (!value.ToLower().Contains("https://"))
                {
                    specialLog.Warn($"Set a URL to {value} with no HTTPS. Callstack: {Environment.StackTrace}"); //This is triggered on NHibernate read always
                }

                pURL = value;
            }
        }
SMartDev
  • 72
  • 1
  • 8
  • Hi, there. I think you need this: https://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method. – wannadream Dec 27 '18 at 18:54
  • @wannadream - I was able to get something working by looking at the stack, thanks! – SMartDev Jan 24 '19 at 19:02

1 Answers1

3

You need to change mapping to let the NHibernate know that you want to use "field" access strategy for this property. This way NHibernate would bypass the setters and set the field directly avoiding any custom logic.

If you are using hbm.xml mapping you can change the mapping for the porperty as following:

<property name="pURL" column="URL" access="field">

And for mapping-by-code:

c.Property("pURL", m =>
{
    m.Column("URL");
    m.Access(Accessor.Field);
});
hazzik
  • 13,019
  • 9
  • 47
  • 86