Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 18, 2025

Users need to initialize entity state from external sources (databases, blob storage) which require async operations. The existing InitializeState method only supports synchronous initialization.

Changes

  • Added InitializeStateAsync virtual method returning ValueTask<TState> for async initialization
  • Updated RunAsync to await InitializeStateAsync instead of calling synchronous InitializeState
  • Preserved backward compatibility - default InitializeStateAsync calls existing InitializeState method
  • Added unit tests covering async initialization patterns (Task, ValueTask, with/without actual async work)

Usage

protected override async ValueTask<MyState> InitializeStateAsync(TaskEntityOperation operation)
{
    var data = await LoadFromDatabaseAsync();
    return new MyState(data.PropA, data.PropB);
}

Existing synchronous overrides of InitializeState continue to work unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>Entities: Make InitializeState support Async operations</issue_title>
<issue_description>I have a requirement to create initial Entity state from an external source (db, blob, etc).
However currently, the override to create initial state for an Entity does not support async operations, forcing me to do syncronous calls.

Suggestion

protected override async Task<T> InitializeStateAsync(TaskEntityOperation operation)
{
     //perform async operations
}

````</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@jviau</author><body>
You can workaround this by using an `EnsureInitializeAsync` pattern. `InitializeState` will kick off the task to populate the state, and then each operation will `await` that task.

``` CSharp
private readonly Task? _initialization;

public async Task MyOperation()
{
    await EnsureInitializedAsync();
}

protected T InitializeState(..)
{
   async InitializeAsync()
   {
        // async work
   }

    _initialization = InitializeAsync();
    return default(T); // InitializeAsync 
}

private Task InitializeAsync()
{
    // perform async work
    this.State = T; // set this.State
}

private ValueTask EnsureInitializedAsync()
{
    return _initialization is null ? default : new(_initialization);
}
```</body></comment_new>
<comment_new><author>@jviau</author><body>
@gorillapower

Yes, returning `default(T)` would be fine there. `InitializeState` is just a convenience method for populating state when there is none. I updated my sample to show that.

You could also set it in `InitializeState` and then populate its contents in `InitializeAsync`.</body></comment_new>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Make InitializeState support async operations Entities: Add InitializeStateAsync for async state initialization Dec 18, 2025
Copilot AI requested a review from YunchuWang December 18, 2025 11:44
@YunchuWang YunchuWang closed this Dec 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Entities: Make InitializeState support Async operations

2 participants