C
C#5mo 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);
}
}
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?
2 Replies
Angius
Angius5mo ago
Just... don't call it the second time?
SWEETPONY
SWEETPONYOP5mo ago
🙂 but how to get changes after update? ah I know, thanks
Want results from more Discord servers?
Add your server