ODataConnectedService icon indicating copy to clipboard operation
ODataConnectedService copied to clipboard

Implement change detection logic.

Open vladascibulskis opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe.

I have an application that uses OData as it's back end service interface. (let's call it Srv1) I need to keep some configuration in sync between 2 applications. Source application (let's call it App1) and Srv1 application.

To do that I follow these steps:

  1. Get current entities from Srv1 (ex. new DataServiceCollection<ConfigItem>(this.odataProxy.ConfigItems, TrackingMode.AutoChangeTracking)
  2. Update them in memory to latest values (received from App1)
  3. Save changes back to Srv1 (ex. this.odataProxy.SaveChanges())

This works fine, but the problem is that when I update retrieved entities, I "touch" all of them and during save all of them are being sent back to the OData service even if values haven't actually changed.

This is not a big deal for small set of items, but when there are few thousand items that need to be synchronized, and most of the time they do not have any changes, it seems unreasonable to spend time on all that processing.

Describe the solution you'd like

My suggestion is to allow to include change tracking in generated client proxy classes.

For example currently proxy generates following property:

public virtual string EquipmentType { get { return this._EquipmentType; } set { this.OnEquipmentTypeChanging(value); this._EquipmentType = value; this.OnEquipmentTypeChanged(); this.OnPropertyChanged("EquipmentType"); } }

I would like to have following:

` public virtual string EquipmentType { get { return this._EquipmentType; } set { if (this._EquipmentType == value) { return; }

            this.OnEquipmentTypeChanging(value);
            this._EquipmentType = value;
            this.OnEquipmentTypeChanged();
            this.OnPropertyChanged("EquipmentType");
        }
    }

`

This is a solution I use currently, but I have to manually tweak the T4 template in order to generate my classes this way. Would be nice to have a checkbox that would allow to turn this feature on/off natively during Connected Service generation.

Describe alternatives you've considered

I could implement change detection logic in my own code, but this seems like a workaround rather than a solution for the root cause.

Additional context

vladascibulskis avatar Nov 11 '21 09:11 vladascibulskis

Hi @vladascibulskis thanks for sharing this. Let me see if I understand the issue:

  • The existing change-tracking implementation triggers a change notification when a property is set even when the value itself has not changed (i.e. the new value is the same as the old value)
  • Your application synchronizes entities, and part of the sync process involves updating in-memory values based on fetched values from one service. These updates may involve updating properties with the same values they already have
  • This causes even those entities whose property values have not changed to be marked as changed and sent over to the second service. This is a waste of resources you want to avoid
  • You're currently manually checking whether values have changed before triggering change notification
  • You suggest that the above solution should be automatically handled by code generator

Is this a correct understanding of your issue?

habbes avatar Jan 25 '22 13:01 habbes

@habbes everything is correct. Only note I would add about this: "You're currently manually checking whether values have changed before triggering change notification"

I'm not checking this manually, but rather I edited T4 template that generates OData proxy class to include change checking code in the generated proxy class, so my application/service code is not affected by this change (except for improved performance). That is why I'm expecting that instead of me modifying T4 template manually you could have this option in default implementation which is deployed/generated when developer adds OData connected service.

vladascibulskis avatar Jan 25 '22 13:01 vladascibulskis

Would you consider contributing a PR to the main repo to add the suggested solution?

habbes avatar Jan 25 '22 18:01 habbes