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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
registered as transient rather than singleton. This allows `CommandHandler` implementations to use
dependencies registered as scoped.

- Reintroduce `IProjectionFactory` in a slightly modified version to allow consumers to make additional "initialization" of projections.

## [1.17.0] - 2025-03-21

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static EventStoreOptionsBuilder UseCQRS(
builder.Services.AddSingleton<IProjectionOptionsFactory, ProjectionOptionsFactory>();

builder.Services.AddSingleton(typeof(ProjectionMetadata<>), typeof(ProjectionMetadata<>));
builder.Services.AddTransient<IProjectionFactory, DefaultProjectionFactory>();

builder.Services.TryAddSingleton<IProjectionTelemetry, ProjectionTelemetry>();

Expand Down
40 changes: 40 additions & 0 deletions src/Atc.Cosmos.EventStore.Cqrs/ProjectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.Extensions.DependencyInjection;

namespace Atc.Cosmos.EventStore.Cqrs;

/// <summary>
/// Responsible for creating <see cref="IProjection"/> instances.
/// </summary>
public interface IProjectionFactory
{
/// <summary>
/// Creates a projection of type <typeparamref name="TProjection"/> for the event stream
/// identified by <paramref name="streamId"/>.
/// </summary>
/// <param name="streamId">ID of the stream being projected.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <typeparam name="TProjection">Type of projection to create.</typeparam>
/// <returns>The created projection.</returns>
public Task<IProjection> CreateAsync<TProjection>(EventStreamId streamId, CancellationToken cancellationToken)
where TProjection : IProjection;
}

/// <summary>
/// The default projection factory which just creates projections by
/// getting them from the DI-container.
/// </summary>
internal sealed class DefaultProjectionFactory : IProjectionFactory
{
private readonly IServiceProvider serviceProvider;

public DefaultProjectionFactory(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}

public Task<IProjection> CreateAsync<TProjection>(EventStreamId streamId, CancellationToken cancellationToken)
where TProjection : IProjection
{
return Task.FromResult<IProjection>(serviceProvider.GetRequiredService<TProjection>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ internal class ProjectionProcessor<TProjection> : IProjectionProcessor<TProjecti
private readonly IReadOnlyCollection<ProjectionFilter> filters;
private readonly IProjectionTelemetry telemetry;
private readonly ProjectionMetadata<TProjection> projectionMetadata;
private readonly IServiceProvider serviceProvider;
private readonly IServiceScopeFactory serviceScopeFactory;
private readonly string projectionName;

public ProjectionProcessor(
IProjectionOptionsFactory optionsFactory,
IProjectionTelemetry telemetry,
ProjectionMetadata<TProjection> projectionMetadata,
IServiceProvider serviceProvider)
IServiceScopeFactory serviceScopeFactory)
{
this.telemetry = telemetry;
this.projectionMetadata = projectionMetadata;
this.serviceProvider = serviceProvider;
this.serviceScopeFactory = serviceScopeFactory;
filters = optionsFactory
.GetOptions<TProjection>()
.Filters;
Expand Down Expand Up @@ -49,10 +49,6 @@ public async Task<ProjectionAction> ProcessBatchAsync(

foreach (var events in groupedEvents)
{
await using var scope = serviceProvider.CreateAsyncScope();

var projection = scope.ServiceProvider.GetRequiredService<TProjection>();

using var operation = batchTelemetry.StartProjection(events.Key);

if (!projectionMetadata.CanConsumeOneOrMoreEvents(events))
Expand All @@ -62,11 +58,16 @@ public async Task<ProjectionAction> ProcessBatchAsync(
continue;
}

var eventStreamId = EventStreamId.FromStreamId(events.Key);
await using var scope = serviceScopeFactory.CreateAsyncScope();
var projectionFactory = scope.ServiceProvider.GetRequiredService<IProjectionFactory>();
var projection = await projectionFactory.CreateAsync<TProjection>(eventStreamId, cancellationToken);

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.

cancellationToken)
.ConfigureAwait(false);

Expand Down
Loading