Skip to content

Commit 78a0df7

Browse files
committed
Autofac sample
1 parent c9b9413 commit 78a0df7

16 files changed

+253
-11
lines changed

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pull_requests:
44
branches:
55
only:
66
- master
7-
image: Visual Studio 2017
7+
image: Visual Studio 2019
88
configuration: Release
99
dotnet_csproj:
1010
patch: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using Autofac;
5+
using NEventLite.Repository;
6+
using NEventLite.Util;
7+
8+
namespace NEventLite.Extensions.Autofac
9+
{
10+
public static class Extensions
11+
{
12+
public static void ScanAndRegisterAggregates(this ContainerBuilder services)
13+
{
14+
services.ScanAndRegisterAggregates(AppDomain.CurrentDomain.GetAssemblies());
15+
}
16+
17+
public static void ScanAndRegisterAggregates(this ContainerBuilder services, IList<Assembly> assemblies)
18+
{
19+
foreach (var a in assemblies.GetAllAggregates())
20+
{
21+
services.RegisterAggregate(a);
22+
}
23+
}
24+
25+
public static void RegisterAggregate(this ContainerBuilder services, AggregateInformation a)
26+
{
27+
// Register full generic types
28+
services.RegisterType(a.Snapshot != null
29+
? typeof(Repository<,,,,>).MakeGenericType(a.Aggregate, a.Snapshot, a.AggregateKey, a.EventKey, a.SnapshotKey)
30+
: typeof(EventOnlyRepository<,,>).MakeGenericType(a.Aggregate, a.AggregateKey, a.EventKey))
31+
.As(typeof(IRepository<,,>).MakeGenericType(a.Aggregate, a.AggregateKey, a.EventKey))
32+
.InstancePerLifetimeScope();
33+
34+
services.RegisterType(typeof(Session<,,>).MakeGenericType(a.Aggregate, a.AggregateKey, a.EventKey))
35+
.As(typeof(ISession<,,>).MakeGenericType(a.Aggregate, a.AggregateKey, a.EventKey))
36+
.InstancePerLifetimeScope();
37+
38+
// Register the convenience GUID scoped ISession interface as well
39+
if (a.AggregateKey == typeof(Guid) && a.EventKey == typeof(Guid))
40+
{
41+
services.RegisterType(typeof(Session<>).MakeGenericType(a.Aggregate))
42+
.As(typeof(ISession<>).MakeGenericType(a.Aggregate))
43+
.InstancePerLifetimeScope();
44+
}
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Autofac" Version="5.1.2" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\NEventLite\NEventLite.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

src/NEventLite.sln

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NEventLite.Samples.Common",
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NEventLite.StorageProviders.EventStore", "Storage\NEventLite.StorageProviders.EventStore\NEventLite.StorageProviders.EventStore.csproj", "{F040E6CE-2C15-4CAB-882F-411D79DD0485}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NEventLite.Extensions.Microsoft.DependencyInjection", "Extensions\NEventLite.Extensions.Microsoft.DependencyInjection\NEventLite.Extensions.Microsoft.DependencyInjection.csproj", "{BFF92C86-54A3-4005-B65F-8F3F3FC6C892}"
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NEventLite.Extensions.Microsoft.DependencyInjection", "Extensions\NEventLite.Extensions.Microsoft.DependencyInjection\NEventLite.Extensions.Microsoft.DependencyInjection.csproj", "{BFF92C86-54A3-4005-B65F-8F3F3FC6C892}"
19+
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NEventLite.Samples.Autofac", "Samples\NEventLite.Samples.Autofac\NEventLite.Samples.Autofac.csproj", "{CACC4756-226D-4664-920A-53C316783303}"
21+
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NEventLite.Extensions.Autofac", "Extensions\NEventLite.Extensions.Autofac\NEventLite.Extensions.Autofac.csproj", "{165E04FD-242E-4446-A57E-4FE690DF86DE}"
1923
EndProject
2024
Global
2125
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -51,6 +55,14 @@ Global
5155
{BFF92C86-54A3-4005-B65F-8F3F3FC6C892}.Debug|Any CPU.Build.0 = Debug|Any CPU
5256
{BFF92C86-54A3-4005-B65F-8F3F3FC6C892}.Release|Any CPU.ActiveCfg = Release|Any CPU
5357
{BFF92C86-54A3-4005-B65F-8F3F3FC6C892}.Release|Any CPU.Build.0 = Release|Any CPU
58+
{CACC4756-226D-4664-920A-53C316783303}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59+
{CACC4756-226D-4664-920A-53C316783303}.Debug|Any CPU.Build.0 = Debug|Any CPU
60+
{CACC4756-226D-4664-920A-53C316783303}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{CACC4756-226D-4664-920A-53C316783303}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{165E04FD-242E-4446-A57E-4FE690DF86DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63+
{165E04FD-242E-4446-A57E-4FE690DF86DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{165E04FD-242E-4446-A57E-4FE690DF86DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
65+
{165E04FD-242E-4446-A57E-4FE690DF86DE}.Release|Any CPU.Build.0 = Release|Any CPU
5466
EndGlobalSection
5567
GlobalSection(SolutionProperties) = preSolution
5668
HideSolutionNode = FALSE

src/NEventLite/Core/IEventPublisher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IEventPublisher
99
Task PublishAsync<TAggregate, TAggregateKey, TEventKey>(IEvent<TAggregate, TAggregateKey, TEventKey> @event) where TAggregate : AggregateRoot<TAggregateKey, TEventKey>;
1010
}
1111

12-
class DefaultNoOpEventPublisher : IEventPublisher
12+
public class DefaultNoOpEventPublisher : IEventPublisher
1313
{
1414
public Task PublishAsync<TAggregate, TAggregateKey, TEventKey>(IEvent<TAggregate, TAggregateKey, TEventKey> @event) where TAggregate : AggregateRoot<TAggregateKey, TEventKey>
1515
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Autofac" Version="5.1.2" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\Extensions\NEventLite.Extensions.Autofac\NEventLite.Extensions.Autofac.csproj" />
14+
<ProjectReference Include="..\..\NEventLite\NEventLite.csproj" />
15+
<ProjectReference Include="..\..\Storage\NEventLite.StorageProviders.EventStore\NEventLite.StorageProviders.EventStore.csproj" />
16+
<ProjectReference Include="..\..\Storage\NEventLite.StorageProviders.InMemory\NEventLite.StorageProviders.InMemory.csproj" />
17+
<ProjectReference Include="..\NEventLite.Samples.Common\NEventLite.Samples.Common.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using Autofac;
5+
using NEventLite.Core;
6+
using NEventLite.Extensions.Autofac;
7+
using NEventLite.Repository;
8+
using NEventLite.Samples.Common;
9+
using NEventLite.Samples.Common.Domain.Schedule;
10+
using NEventLite.Samples.Common.Domain.Schedule.Snapshots;
11+
using NEventLite.Samples.Common.Handlers;
12+
using NEventLite.Storage;
13+
using NEventLite.StorageProviders.InMemory;
14+
using Module = Autofac.Module;
15+
16+
namespace NEventLite.Samples.Autofac
17+
{
18+
public class NEventLiteModule : Module
19+
{
20+
private const int SnapshotFrequency = 2;
21+
22+
protected override void Load(ContainerBuilder builder)
23+
{
24+
//This path is used to save in memory storage
25+
var strTempDataFolderPath = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\";
26+
27+
//create temp directory if it doesn't exist
28+
new FileInfo(strTempDataFolderPath).Directory?.Create();
29+
30+
var inMemoryEventStorePath = $@"{strTempDataFolderPath}events.stream.dump";
31+
var inMemorySnapshotStorePath = $@"{strTempDataFolderPath}events.snapshot.dump";
32+
33+
File.Delete(inMemoryEventStorePath);
34+
File.Delete(inMemorySnapshotStorePath);
35+
36+
builder.RegisterType<DefaultSystemClock>().As<IClock>().InstancePerLifetimeScope();
37+
builder.RegisterType<MyEventPublisher>().As<IEventPublisher>().InstancePerLifetimeScope();
38+
39+
builder.Register(c => new InMemoryEventStorageProvider(inMemoryEventStorePath))
40+
.As<IEventStorageProvider<Guid>>().InstancePerLifetimeScope();
41+
builder.Register(c => new InMemorySnapshotStorageProvider(SnapshotFrequency, inMemorySnapshotStorePath))
42+
.As<ISnapshotStorageProvider<Guid>>().InstancePerLifetimeScope();
43+
44+
builder.ScanAndRegisterAggregates();
45+
46+
//Or add the repository registration manually
47+
//builder.RegisterType<Repository<Schedule, ScheduleSnapshot, Guid, Guid, Guid>>().As<IRepository<Schedule, Guid, Guid>>().InstancePerLifetimeScope();
48+
//builder.RegisterType<Session<Schedule>>().As<ISession<Schedule>>().InstancePerLifetimeScope();
49+
50+
builder.RegisterType<CreateScheduleHandler>();
51+
builder.RegisterType<CreateTodoHandler>();
52+
builder.RegisterType<UpdateTodoNameHandler>();
53+
builder.RegisterType<CompleteTodoHandler>();
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Autofac;
4+
using NEventLite.Repository;
5+
using NEventLite.Samples.Common.Domain.Schedule;
6+
using NEventLite.Samples.Common.Handlers;
7+
using Newtonsoft.Json;
8+
9+
namespace NEventLite.Samples.Autofac
10+
{
11+
public static class Program
12+
{
13+
static void Main(string[] args)
14+
{
15+
RunSample().ConfigureAwait(false).GetAwaiter().GetResult();
16+
}
17+
18+
public static async Task RunSample()
19+
{
20+
var builder = new ContainerBuilder();
21+
builder.RegisterModule<NEventLiteModule>();
22+
var container = builder.Build();
23+
24+
Guid scheduleId;
25+
Guid todoId;
26+
27+
await using (var scope = container.BeginLifetimeScope())
28+
{
29+
var handler = scope.Resolve<CreateScheduleHandler>();
30+
scheduleId = await handler.HandleAsync("test schedule");
31+
}
32+
33+
await using (var scope = container.BeginLifetimeScope())
34+
{
35+
var handler = scope.Resolve<CreateTodoHandler>();
36+
todoId = await handler.HandleAsync(scheduleId, "todo item 1");
37+
}
38+
39+
await using (var scope = container.BeginLifetimeScope())
40+
{
41+
var handler = scope.Resolve<CreateTodoHandler>();
42+
await handler.HandleAsync(scheduleId, "todo item 2");
43+
}
44+
45+
await using (var scope = container.BeginLifetimeScope())
46+
{
47+
var handler = scope.Resolve<CreateTodoHandler>();
48+
await handler.HandleAsync(scheduleId, "todo item 3");
49+
}
50+
51+
await using (var scope = container.BeginLifetimeScope())
52+
{
53+
var handler = scope.Resolve<UpdateTodoNameHandler>();
54+
await handler.HandleAsync(scheduleId, todoId, "todo item 1 updated");
55+
}
56+
57+
await using (var scope = container.BeginLifetimeScope())
58+
{
59+
var handler = scope.Resolve<CompleteTodoHandler>();
60+
await handler.HandleAsync(scheduleId, todoId);
61+
}
62+
63+
await using (var scope = container.BeginLifetimeScope())
64+
{
65+
var session = scope.Resolve<ISession<Schedule>>();
66+
var result = await session.GetByIdAsync(scheduleId);
67+
Console.WriteLine("--------");
68+
Console.WriteLine("Final result after applying all events...");
69+
PrintToConsole(result, ConsoleColor.Green);
70+
}
71+
}
72+
73+
public static void PrintToConsole(object @object, ConsoleColor color)
74+
{
75+
Console.ForegroundColor = color;
76+
Console.WriteLine(JsonConvert.SerializeObject(@object, Formatting.Indented));
77+
Console.ResetColor();
78+
}
79+
}
80+
}

src/Samples/NEventLite.Samples.ConsoleApp/Handlers/CompleteTodoHandler.cs src/Samples/NEventLite.Samples.Common/Handlers/CompleteTodoHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using NEventLite.Repository;
44
using NEventLite.Samples.Common.Domain.Schedule;
55

6-
namespace NEventLite.Samples.ConsoleApp.Handlers
6+
namespace NEventLite.Samples.Common.Handlers
77
{
88
public class CompleteTodoHandler
99
{

src/Samples/NEventLite.Samples.ConsoleApp/Handlers/CreateScheduleHandler.cs src/Samples/NEventLite.Samples.Common/Handlers/CreateScheduleHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using NEventLite.Repository;
44
using NEventLite.Samples.Common.Domain.Schedule;
55

6-
namespace NEventLite.Samples.ConsoleApp.Handlers
6+
namespace NEventLite.Samples.Common.Handlers
77
{
88
public class CreateScheduleHandler
99
{

src/Samples/NEventLite.Samples.ConsoleApp/Handlers/CreateTodoHandler.cs src/Samples/NEventLite.Samples.Common/Handlers/CreateTodoHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using NEventLite.Repository;
44
using NEventLite.Samples.Common.Domain.Schedule;
55

6-
namespace NEventLite.Samples.ConsoleApp.Handlers
6+
namespace NEventLite.Samples.Common.Handlers
77
{
88
public class CreateTodoHandler
99
{

src/Samples/NEventLite.Samples.ConsoleApp/Handlers/UpdateTodoNameHandler.cs src/Samples/NEventLite.Samples.Common/Handlers/UpdateTodoNameHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using NEventLite.Repository;
44
using NEventLite.Samples.Common.Domain.Schedule;
55

6-
namespace NEventLite.Samples.ConsoleApp.Handlers
6+
namespace NEventLite.Samples.Common.Handlers
77
{
88
public class UpdateTodoNameHandler
99
{

src/Samples/NEventLite.Samples.ConsoleApp/MyEventPublisher.cs src/Samples/NEventLite.Samples.Common/MyEventPublisher.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22
using System.Threading.Tasks;
33
using NEventLite.Core;
44
using NEventLite.Core.Domain;
5+
using Newtonsoft.Json;
56

6-
namespace NEventLite.Samples.ConsoleApp
7+
namespace NEventLite.Samples.Common
78
{
89
public class MyEventPublisher : IEventPublisher
910
{
1011
public Task PublishAsync<TAggregate, TAggregateKey, TEventKey>(IEvent<TAggregate, TAggregateKey, TEventKey> @event) where TAggregate : AggregateRoot<TAggregateKey, TEventKey>
1112
{
1213
Console.WriteLine($"Event {@event.TargetVersion + 2}");
13-
Program.PrintToConsole(@event, ConsoleColor.Cyan);
14+
PrintToConsole(@event, ConsoleColor.Cyan);
1415
Console.WriteLine();
1516

1617
return Task.CompletedTask;
1718
}
19+
20+
public static void PrintToConsole(object @object, ConsoleColor color)
21+
{
22+
Console.ForegroundColor = color;
23+
Console.WriteLine(JsonConvert.SerializeObject(@object, Formatting.Indented));
24+
Console.ResetColor();
25+
}
1826
}
1927
}

src/Samples/NEventLite.Samples.Common/NEventLite.Samples.Common.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
9+
</ItemGroup>
10+
711
<ItemGroup>
812
<ProjectReference Include="..\..\NEventLite\NEventLite.csproj" />
913
</ItemGroup>

src/Samples/NEventLite.Samples.ConsoleApp/DependencyInjection.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using NEventLite.Samples.Common;
1111
using NEventLite.Samples.Common.Domain.Schedule;
1212
using NEventLite.Samples.Common.Domain.Schedule.Snapshots;
13-
using NEventLite.Samples.ConsoleApp.Handlers;
13+
using NEventLite.Samples.Common.Handlers;
1414
using NEventLite.Storage;
1515
using NEventLite.StorageProviders.EventStore;
1616
using NEventLite.StorageProviders.EventStore.Core;

src/Samples/NEventLite.Samples.ConsoleApp/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using NEventLite.Samples.Common;
1010
using NEventLite.Samples.Common.Domain.Schedule;
1111
using NEventLite.Samples.Common.Domain.Schedule.Snapshots;
12-
using NEventLite.Samples.ConsoleApp.Handlers;
12+
using NEventLite.Samples.Common.Handlers;
1313
using NEventLite.Storage;
1414
using NEventLite.StorageProviders.EventStore;
1515
using NEventLite.StorageProviders.InMemory;

0 commit comments

Comments
 (0)