✅ How to optimize this method?
I have this method:
the problem is: I need to call
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);
}
}
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);
}
}
FindByIdentityAsync
twice and we need this just to check updated property and send event.
is it possible to remove second FindByIdentityAsync
?2 Replies