Hook into Entity SetValue

Hook into Entity SetValue

Postby ludydoo » Mon 23 Nov 2015 19:31

Hi,

I am still evaluating EntityDAC,

I would like to be able to add an event when a model property changes. In this post, I am using standard delphi events (references to procedures). But in my application, I will use Spring4d multicast events.

So, I followed the instructions on the manual :

I managed to change the BaseClass of my entities and define another

Code: Select all
TMyEntity = class(TMappedEntity)


In Entity Developer, I changed all my classes' base type to TMyEntity.

Then, in this TMyEntity, I have an event :
Code: Select all
fOnPropertyChanged : TPropertyChangedEvent;


and a dispatch method
Code: Select all
procedure NotifyAttributeChanged(const attributeName : string)


Then, in the template, in the proeperty setters, I modified the generation code to trigger the event when a property changes.

Code: Select all
NotifyAttributeChanged('<#= prop.Name #>');


In my application, i can hook into that event to be notified of when an entity attribute changes. It works as long as I am not using a TEntityDataSet to update the entity data.

Ex :

Code: Select all
aModel.Name := 'ANewName' // triggers the event
aDataSet.fieldByName('aFieldName').Value.AsSring := 'AnotherName'; // does not trigger the event


What's probably happening is that the dataset does not use the model getters/setters to update the model attributes.

Being able to use the custom getters and setters both when updating data through the EntityDataSet AND the Entity itself would be great since it would allow some business rules to be validated regardless of the input "method" (DataSet fields or TEntity property setters).

Also, in this case, it would allow to add custom events that would be fired regardless of "How" the model is updated.

If in a form I have some DB aware controls bound to an EntityDataSet with SourceEntity := aEntity. I would also have a button that is enabled only if aEntity.IsBig = true. It would be way easier to simply bind myself to aEntity.OnPropertyChanged event rather than having to both check for the EntityDataSet and Entity changes.

Another example. If I have a UserRights Entity that is accessible through a Service. I could simply bind myself to the UserRights.OnPropertyChanged anywhere in my application. I could then update the ui to deactivate some actions or whatnot.

The reason I am using a dataset is because I have some data aware controls that are not LiveBinding-enabled (DevExpress). We are refactoring our code towards a more OO architecture, thus our interest in evaluating EntityDac.
ludydoo
 
Posts: 6
Joined: Sun 22 Nov 2015 21:53

Re: Hook into Entity SetValue

Postby ludydoo » Mon 23 Nov 2015 19:50

I found how to hook into the "SetValue"

In my base class, I override the method :

Code: Select all
DoAfterAttributeModify(Attribute: TEntityAttribute);


Though, when I update an entity, the method is called twice. Any idea why?
ludydoo
 
Posts: 6
Joined: Sun 22 Nov 2015 21:53

Re: Hook into Entity SetValue

Postby AlexP » Tue 24 Nov 2015 09:50

Hello,

Please send your model and a sample application with the overridden DoAfterAttributeModify method to support*devart*com, since we can't reproduce repeating call of this event on modifying Entity.
AlexP
Devart Team
 
Posts: 5525
Joined: Tue 10 Aug 2010 11:35


Return to EntityDAC