Skip to content

Bring back ProjectionFactory #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2025

Conversation

agehrke
Copy link
Collaborator

@agehrke agehrke commented Apr 7, 2025

Bring back ProjectionFactory but with a slightly different interface. This factory would allow consumers to make additional "initialization" of projections:

public interface IProjectionFactory
{
    public Task<IProjection> CreateAsync<TProjection>(EventStreamId streamId, CancellationToken cancellationToken)
        where TProjection : IProjection;
}

A few things to note:

  • The method is async to allow implementations to do I/O
  • Returns IProjection rather than TProjection - the ProjectionProcessor does not need to actual type. Returning IProjection allows factory implementations to return a decorated TProjection - however it may be cumbersome to decorate an IProjection implementation when they consume a lot of event types.
  • Takes an EventStreamId

The default implementation DefaultProjectionFactory just resolves TProjection through IServiceProvider. To allow a IProjectionFactory to create instances that consumes scoped resources, the type is registered as transient.

One could argue that the IProjection.InitializeAsync method could be invoked by the DefaultProjectionFactory as that method is somewhat an async constructor.

Other changes

I have also changed ProjectionProcessor to use IServiceScopeFactory instead of IServiceProvider which, IMO, make it more clear that we are only creating scopes.

Also use IServiceScopeFactory instead of IServiceProvider in
ProjectionProcessor to make it more clear that we are only creating scopes
@agehrke
Copy link
Collaborator Author

agehrke commented Apr 7, 2025

If my PR #66 gets merged I could create some functional tests to show how the IProjectionFactory could be used.

@christianhelle christianhelle self-assigned this Apr 8, 2025
@christianhelle christianhelle added the enhancement New feature or request label Apr 8, 2025
@christianhelle christianhelle requested a review from Copilot April 8, 2025 07:39
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Copy link
Member

@christianhelle christianhelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agehrke Please update the change log

try
{
await projection
.InitializeAsync(
events.Key,
eventStreamId,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this as something slightly more costly than just passing in the events.Key, but I don't think it matters

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no. Previously there was an implicit conversion from StreamId to EventStreamId. Now I'm just explicit about that conversion.

christianhelle
christianhelle previously approved these changes Apr 8, 2025
@agehrke
Copy link
Collaborator Author

agehrke commented Apr 8, 2025

@agehrke Please update the change log

I have updated changelog.md

@christianhelle christianhelle merged commit d6d4def into atc-net:main Apr 8, 2025
1 check passed
@agehrke agehrke deleted the projectionfactory branch April 8, 2025 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants