diff --git a/src/ContainerScopedProjectionTests/projections_with_IoC_services.cs b/src/ContainerScopedProjectionTests/projections_with_IoC_services.cs index 4072fbeb13..5864ea584c 100644 --- a/src/ContainerScopedProjectionTests/projections_with_IoC_services.cs +++ b/src/ContainerScopedProjectionTests/projections_with_IoC_services.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.Linq; +using System.Threading; using System.Threading.Tasks; using JasperFx.Core; using JasperFx.Core.Reflection; @@ -361,6 +362,45 @@ public async Task get_async_shards_with_custom_name_on_martenStore() } + + [Fact] + public async Task use_multistream_projection_as_scoped_and_inline_on_martenStore() + { + using var host = await Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder() + .ConfigureServices(services => + { + services.AddSingleton(); + + services.AddMartenStore(opts => + { + opts.Connection(ConnectionSource.ConnectionString); + opts.DatabaseSchemaName = "ioc3"; + opts.ApplyChangesLockId = opts.ApplyChangesLockId + 9; + }).AddProjectionWithServices(ProjectionLifecycle.Inline, ServiceLifetime.Scoped, "MyProjection") + .ApplyAllDatabaseChangesOnStartup(); + }).StartAsync(); + + var store = host.Services.GetRequiredService(); + + + + await using var session = store.LightweightSession(); + var streamId = session.Events.StartStream(new ProductRegistered("Ankle Socks", "Socks")).Id; + await session.SaveChangesAsync(); + + var product = await session.LoadAsync(streamId); + product.Price.ShouldBeGreaterThan(0); + product.Name.ShouldBe("Ankle Socks"); + + // Now rebuild + var daemon = await store.BuildProjectionDaemonAsync(); + await daemon.RebuildProjectionAsync(CancellationToken.None); + + // Test again + product = await session.LoadAsync(streamId); + product.Price.ShouldBeGreaterThan(0); + product.Name.ShouldBe("Ankle Socks"); + } } public interface IPriceLookup @@ -417,6 +457,37 @@ public override Product Evolve(Product snapshot, Guid id, IEvent e) #endregion + +#region sample_MultiStreamProjection + +public class ProductMultiStreamProjection: SingleStreamProjection +{ + private readonly IPriceLookup _lookup; + + // The lookup service would be injected by IoC + public ProductMultiStreamProjection(IPriceLookup lookup) + { + _lookup = lookup; + Name = "Product"; + } + + public override Product Evolve(Product snapshot, Guid id, IEvent e) + { + snapshot ??= new Product { Id = id }; + + if (e.Data is ProductRegistered r) + { + snapshot.Price = _lookup.PriceFor(r.Category); + snapshot.Name = r.Name; + snapshot.Category = r.Category; + } + + return snapshot; + } +} + +#endregion + public interface ICustomStore: IDocumentStore { } diff --git a/src/Marten/Marten.csproj b/src/Marten/Marten.csproj index 8324d5872d..8d731b5bf0 100644 --- a/src/Marten/Marten.csproj +++ b/src/Marten/Marten.csproj @@ -33,9 +33,9 @@ - - - + + + @@ -43,5 +43,11 @@ + + + + + +