how to: simplified/individual change notification
Posted: Thu 22 Dec 2011 17:50
The "Property Change Notifiers" generate far too much code and too many function calls for my taste - all I need is a notification that a property has been changed, and only for a few, select properties.
Add the following extended property to your template:
Near the beginning of the GenerateProperty() method, add the following:
Within that method, find the following line:
Immediately after that line, paste in the following:
That enables you to turn on the callback generation for a specific property.
Next, find the GenerateExtensibilityMethodDefinitions method and select the following section of code:
For a property with change-notification turned on, the generated code looks something like this:
Note that this works best with the "field" or "nosetter" access strategy, as both of these avoid invoking the set-method when NH hydrates an object with values from the database - using "property" access strategy will cause the callback to be invoked when you load an object, which is probably not what you want.
Also note that this is currently only implemented for scalar properties - not for navigation properties. You need it for navigation properties, you could probably follow this example and make the change yourself.
Hope this is useful to somebody else
Add the following extended property to your template:
Code: Select all
Code: Select all
bool propCallback = (bool) property.GetProperty("PropertyChangeCallback");
Code: Select all
this._ = value;
Code: Select all
this.OnChanged();
Next, find the GenerateExtensibilityMethodDefinitions method and select the following section of code:
Code: Select all
if (supportPropertyChanging) {
string defaultNamespace = codeProvider.GetValidIdentifier(model.GetDefaultNamespace());
foreach (HibernateProperty property in type.Properties ) {
#>
partial void OnChanging( value);
partial void OnChanged();
partial void OnChanging( value);
partial void OnChanged();
partial void OnChanging( value);
partial void OnChanged();
partial void OnChanging( value);
partial void OnChanged();
<#+
}
}
}
Code: Select all
private string _Name;
#region Extensibility Method Definitions
partial void OnCreated();
partial void OnNameChanged();
#endregion
protected internal virtual string Name
{
get
{
return this._Name;
}
set
{
this._Name = value;
this.OnNameChanged();
}
}
Also note that this is currently only implemented for scalar properties - not for navigation properties. You need it for navigation properties, you could probably follow this example and make the change yourself.
Hope this is useful to somebody else
