Skip to content
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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<IPriceLookup, PriceLookup>();

services.AddMartenStore<ICustomStore>(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.DatabaseSchemaName = "ioc3";
opts.ApplyChangesLockId = opts.ApplyChangesLockId + 9;
}).AddProjectionWithServices<ProductMultiStreamProjection>(ProjectionLifecycle.Inline, ServiceLifetime.Scoped, "MyProjection")
.ApplyAllDatabaseChangesOnStartup();
}).StartAsync();

var store = host.Services.GetRequiredService<ICustomStore>();



await using var session = store.LightweightSession();
var streamId = session.Events.StartStream<Product>(new ProductRegistered("Ankle Socks", "Socks")).Id;
await session.SaveChangesAsync();

var product = await session.LoadAsync<Product>(streamId);
product.Price.ShouldBeGreaterThan(0);
product.Name.ShouldBe("Ankle Socks");

// Now rebuild
var daemon = await store.BuildProjectionDaemonAsync();
await daemon.RebuildProjectionAsync<Product>(CancellationToken.None);

// Test again
product = await session.LoadAsync<Product>(streamId);
product.Price.ShouldBeGreaterThan(0);
product.Name.ShouldBe("Ankle Socks");
}
}

public interface IPriceLookup
Expand Down Expand Up @@ -417,6 +457,37 @@ public override Product Evolve(Product snapshot, Guid id, IEvent e)

#endregion


#region sample_MultiStreamProjection

public class ProductMultiStreamProjection: SingleStreamProjection<Product, Guid>
{
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
{
}
12 changes: 9 additions & 3 deletions src/Marten/Marten.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@

<ItemGroup>
<PackageReference Include="FSharp.Core" Version="9.0.100" />
<PackageReference Include="JasperFx" Version="1.5.0" />
<PackageReference Include="JasperFx.Events" Version="1.7.1" />
<PackageReference Include="JasperFx.RuntimeCompiler" Version="4.0.0" />
<!-- <PackageReference Include="JasperFx" Version="1.5.0" />-->
<!-- <PackageReference Include="JasperFx.Events" Version="1.7.1" />-->
<!-- <PackageReference Include="JasperFx.RuntimeCompiler" Version="4.0.0" />-->
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<!-- This is forced by Npgsql peer dependency -->
<PackageReference Include="Npgsql.Json.NET" Version="9.0.2" />
<PackageReference Include="Polly.Core" Version="8.5.2" />
<PackageReference Include="Weasel.Postgresql" Version="8.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\jasperfx\src\JasperFx.Events\JasperFx.Events.csproj" />
<ProjectReference Include="..\..\..\jasperfx\src\JasperFx.RuntimeCompiler\JasperFx.RuntimeCompiler.csproj" />
<ProjectReference Include="..\..\..\jasperfx\src\JasperFx\JasperFx.csproj" />
</ItemGroup>

<Import Project="../../Analysis.Build.props" />
</Project>
Loading