Skip to content

Commit 74b5a35

Browse files
committed
fleshing out backend
1 parent a54d491 commit 74b5a35

File tree

4 files changed

+110
-17
lines changed

4 files changed

+110
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="GenericChildPerEntityParent.cs" company="Akka.NET Project">
3+
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net>
4+
// </copyright>
5+
// -----------------------------------------------------------------------
6+
7+
using Akka.Actor;
8+
using Akka.Cluster.Sharding;
9+
10+
namespace CqrsSqlServer.Backend.Actors;
11+
12+
/// <summary>
13+
/// A generic "child per entity" parent actor.
14+
/// </summary>
15+
/// <remarks>
16+
/// Intended for simplifying unit tests where we don't want to use Akka.Cluster.Sharding.
17+
/// </remarks>
18+
public sealed class GenericChildPerEntityParent : ReceiveActor
19+
{
20+
public static Props Props(IMessageExtractor extractor, Func<string, Props> propsFactory)
21+
{
22+
return Akka.Actor.Props.Create(() => new GenericChildPerEntityParent(extractor, propsFactory));
23+
}
24+
25+
/*
26+
* Re-use Akka.Cluster.Sharding's infrastructure here to keep things simple.
27+
*/
28+
private readonly IMessageExtractor _extractor;
29+
private Func<string, Props> _propsFactory;
30+
31+
public GenericChildPerEntityParent(IMessageExtractor extractor, Func<string, Props> propsFactory)
32+
{
33+
_extractor = extractor;
34+
_propsFactory = propsFactory;
35+
36+
ReceiveAny(o =>
37+
{
38+
var result = _extractor.EntityId(o);
39+
if (result is null) return;
40+
Context.Child(result).GetOrElse(() => Context.ActorOf(propsFactory(result), result)).Forward(_extractor.EntityMessage(o));
41+
});
42+
}
43+
}

src/cqrs/cqrs-sqlserver/CqrsSqlServer.Backend/CqrsSqlServer.Backend.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<ProjectReference Include="..\CqrsSqlServer.DataModel\CqrsSqlServer.DataModel.csproj" />
1112
<ProjectReference Include="..\CqrsSqlServer.Shared\CqrsSqlServer.Shared.csproj"/>
1213
</ItemGroup>
1314

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,52 @@
1-
// See https://aka.ms/new-console-template for more information
1+
using Akka.Actor;
2+
using Akka.Hosting;
3+
using Akka.Persistence.SqlServer.Hosting;
4+
using CqrsSqlServer.Backend.Actors;
5+
using CqrsSqlServer.DataModel;
6+
using CqrsSqlServer.Shared.Serialization;
7+
using CqrsSqlServer.Shared.Sharding;
8+
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Hosting;
212

3-
Console.WriteLine("Hello, World!");
13+
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
14+
15+
var hostBuilder = new HostBuilder()
16+
.ConfigureAppConfiguration((hostContext, configApp) =>
17+
{
18+
configApp.AddEnvironmentVariables()
19+
.AddJsonFile("appsettings.json")
20+
.AddJsonFile($"appsettings.{environment}.json");
21+
})
22+
.ConfigureServices((hostContext, services) =>
23+
{
24+
var connectionString = hostContext.Configuration.GetConnectionString("AkkaSqlConnection");
25+
if (connectionString is null)
26+
throw new Exception("AkkaSqlConnection setting is missing");
27+
28+
services.AddDbContext<CqrsSqlServerContext>(options =>
29+
{
30+
// disable change tracking for all implementations of this context
31+
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
32+
options.UseSqlServer(connectionString);
33+
});
34+
35+
services.AddAkka("CqrsProjections", (builder, provider) =>
36+
{
37+
builder
38+
.AddAppSerialization()
39+
.WithSqlServerPersistence(connectionString)
40+
.WithActors((system, registry, resolver) =>
41+
{
42+
var parentActor =
43+
system.ActorOf(
44+
Props.Create(() => new GenericChildPerEntityParent(new ProductMessageRouter(),
45+
ProductTotalsActor.GetProps)), "productTotals");
46+
47+
registry.Register<ProductTotalsActor>(parentActor);
48+
});
49+
});
50+
});
51+
52+
await hostBuilder.Build().RunAsync();
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
// using Akka.Hosting;
2-
//
3-
// namespace CqrsSqlServer.Shared.Serialization;
4-
//
5-
// public static class SerializationHostingExtensions
6-
// {
7-
// /// <summary>
8-
// /// Configures the custom serialization for the SqlSharding App.
9-
// /// </summary>
10-
// public static AkkaConfigurationBuilder AddAppSerialization(this AkkaConfigurationBuilder builder)
11-
// {
12-
// return builder.WithCustomSerializer("sql-sharding", new[] { typeof(ISqlShardingProtocolMember) },
13-
// system => new MessageSerializer(system));
14-
// }
15-
// }
1+
using Akka.Hosting;
2+
3+
namespace CqrsSqlServer.Shared.Serialization;
4+
5+
public static class SerializationHostingExtensions
6+
{
7+
/// <summary>
8+
/// Configures the custom serialization for the SqlSharding App.
9+
/// </summary>
10+
public static AkkaConfigurationBuilder AddAppSerialization(this AkkaConfigurationBuilder builder)
11+
{
12+
return builder.WithCustomSerializer("sql-sharding", new[] { typeof(ISqlShardingProtocolMember) },
13+
system => new MessageSerializer(system));
14+
}
15+
}

0 commit comments

Comments
 (0)