microsoftgraph / msgraph-sdk-dotnet

Microsoft Graph Client Library for .NET!
https://graph.microsoft.com
Other
703 stars 249 forks source link

Improved Handling of Unchanged Objects in Graph API .NET SDK #1905

Open plamber opened 1 year ago

plamber commented 1 year ago

I would like to propose an enhancement to the Graph API .NET SDK that enables better handling of unchanged objects during patch and post operations. Currently, even if there are no changes in the object being updated, the patch operation is still executed. This feature request aims to provide a mechanism for identifying if an object has updated values and to prevent unnecessary execution of patch and post operations when no changes are present.

Maybe there is already a technique that can be used to accomplish this goal.

Describe the solution you'd like The suggested solution involves introducing two key features: the ability to identify if an object has updated values and the option to skip patch and post operations when no changes exist in the backing store. By implementing these features, developers can improve the efficiency and reduce unnecessary API calls in scenarios where object updates are conditional based on changes.

var userId = "putYourUserId";
var user = await graphClient.Users[userId].GetAsync();

// Check if the user object has any updated values
bool hasChanges = user.HasChanges();

if (hasChanges)
{
    // Implement the business logic to perform updates on the object

    // Perform the patch operation only if there are changes
    await graphClient.Users[user.Id].PatchAsync(user);
}
else
{
    // No changes, skip the patch operation
    Console.WriteLine("No changes found in the user object. Skipping patch operation.");
}

In the example above, the HasChanges() method is introduced to determine if the object contains any modified properties. This method compares the object's current state with the original values stored in the backing store to identify changes.

With this enhancement, developers gain more control over when to execute patch and post operations, preventing unnecessary API calls and improving performance in scenarios where updates are conditional.

andrueastman commented 1 year ago

Thanks for raising this @plamber

I believe this API would be better placed in the BackingStore itself to change the suggestion to

bool hasChanges = user.BackingStore.HasChanges();

At the moment, I believe you can get away with an extension method close to this.

    public static bool HasChanges(this IBackingStore store)
    {
        store.ReturnOnlyChangedValues = true;
        return store. Enumerate().Any();
    }
plamber commented 1 year ago

Hi @andrueastman, I ended up creating this extension method.

public static class GraphExtensions
{
    public static bool HasChanges(this IBackingStore store)
    {
        bool originalValue = store.ReturnOnlyChangedValues;
        store.ReturnOnlyChangedValues = true;
        try
        {
            return store.Enumerate().Any();
        }
        finally
        {
            store.ReturnOnlyChangedValues = originalValue;
        }
    }
}

Thank you for your help.

Cheers