C#C
C#2y ago
SWEETPONY

✅ How to optimize this method?

I have this method:
protected override async Task UpdateCoreAsync(WorkingTaskModel workingTask, EventHandlingContext context)
    {
        var exist = await FindByIdentityAsync(workingTask.Identity, context).ConfigureAwait(false);
        if(exist == null)
            return;
        if(await UpdateInternalAsync(workingTask, exist, context).ConfigureAwait(false))
        {
            var stateIsChanged = exist.State != workingTask.State;
            if(stateIsChanged)
            {
                var historyModel = new WorkingTaskHistoryModel
                {
                    ChangedData = new ChangedWorkingTaskModel(){State = workingTask.State},
                    UserIdentity = context.Context.UserAccountIdentity,
                    SourceName = context.Context.SourceName,
                    UpdatedAt = timeProvider.GetUtcNow().DateTime
                };

                await UpdateWorkingTaskHistoryAsync(historyModel, exist, context).ConfigureAwait(false);
            }

            var found = await FindByIdentityAsync(workingTask.Identity, context).ConfigureAwait(false);
            await eventDispatcher.WorkingTaskUpdatedAsync(found!, exist, context).ConfigureAwait(false);
        }
    }


the problem is: I need to call FindByIdentityAsync twice and we need this just to check updated property and send event.
is it possible to remove second FindByIdentityAsync?
Was this page helpful?