© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
3 replies
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
FindByIdentityAsync
twice and we need this just to check updated property and send event.
is it possible to remove second
FindByIdentityAsync
FindByIdentityAsync
?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to optimize this?
C#CC# / help
3y ago
✅ How to optimize this code?
C#CC# / help
16mo ago
❔ Need help to optimize this simulation
C#CC# / help
3y ago