Skip to content

Commit 0ae3211

Browse files
authored
Merge pull request #1 from XerProjects/feature-autofac
Feature autofac
2 parents 4ee9176 + 85378b4 commit 0ae3211

26 files changed

+829
-29
lines changed

.gitattributes

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain

.gitignore

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
## Ignore Visual Studio temporary files, build results, and
22
## files generated by popular Visual Studio add-ons.
3-
##
4-
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
53

64
# User-specific files
75
*.suo
@@ -42,11 +40,10 @@ TestResult.xml
4240
[Rr]eleasePS/
4341
dlldata.c
4442

45-
# .NET Core
43+
# DNX
4644
project.lock.json
4745
project.fragment.lock.json
4846
artifacts/
49-
**/Properties/launchSettings.json
5047

5148
*_i.c
5249
*_p.c
@@ -113,10 +110,6 @@ _TeamCity*
113110
# DotCover is a Code Coverage Tool
114111
*.dotCover
115112

116-
# Visual Studio code coverage results
117-
*.coverage
118-
*.coveragexml
119-
120113
# NCrunch
121114
_NCrunch_*
122115
.*crunch*.local.xml
@@ -150,7 +143,7 @@ publish/
150143
*.azurePubxml
151144
# TODO: Comment the next line if you want to checkin your web deploy settings
152145
# but database connection strings (with potential passwords) will be unencrypted
153-
*.pubxml
146+
#*.pubxml
154147
*.publishproj
155148

156149
# Microsoft Azure Web App publish settings. Comment the next line if you want to
@@ -166,7 +159,7 @@ PublishScripts/
166159
!**/packages/build/
167160
# Uncomment if necessary however generally it will be regenerated when needed
168161
#!**/packages/repositories.config
169-
# NuGet v3's project.json files produces more ignorable files
162+
# NuGet v3's project.json files produces more ignoreable files
170163
*.nuget.props
171164
*.nuget.targets
172165

@@ -199,6 +192,7 @@ ClientBin/
199192
*.jfm
200193
*.pfx
201194
*.publishsettings
195+
node_modules/
202196
orleans.codegen.cs
203197

204198
# Since there are multiple workflows, uncomment next line to ignore bower_components
@@ -219,7 +213,6 @@ UpgradeLog*.htm
219213
# SQL Server files
220214
*.mdf
221215
*.ldf
222-
*.ndf
223216

224217
# Business Intelligence projects
225218
*.rdl.data
@@ -234,20 +227,13 @@ FakesAssemblies/
234227

235228
# Node.js Tools for Visual Studio
236229
.ntvs_analysis.dat
237-
node_modules/
238-
239-
# Typescript v1 declaration files
240-
typings/
241230

242231
# Visual Studio 6 build log
243232
*.plg
244233

245234
# Visual Studio 6 workspace options file
246235
*.opt
247236

248-
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
249-
*.vbw
250-
251237
# Visual Studio LightSwitch build output
252238
**/*.HTMLClient/GeneratedArtifacts
253239
**/*.DesktopClient/GeneratedArtifacts
@@ -274,15 +260,9 @@ paket-files/
274260
__pycache__/
275261
*.pyc
276262

277-
# Cake - Uncomment if you are using it
278-
# tools/**
279-
# !tools/packages.config
280-
281-
# Telerik's JustMock configuration file
282-
*.jmconfig
263+
# VS Code
264+
.vscode/
283265

284-
# BizTalk build output
285-
*.btp.cs
286-
*.btm.cs
287-
*.odx.cs
288-
*.xsd.cs
266+
tools/**
267+
!tools/packages.config
268+
BuildArtifacts/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Allow test project to access internal classes.
2+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Xer.Cqrs.Extensions.Autofac.Tests")]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Xer.Delegator;
3+
4+
namespace Xer.Cqrs.Extensions.Autofac
5+
{
6+
internal class CommandHandlerDelegateResolver : IMessageHandlerResolver
7+
{
8+
private readonly IMessageHandlerResolver _messageHandlerResolver;
9+
10+
internal CommandHandlerDelegateResolver(IMessageHandlerResolver messageHandlerResolver)
11+
{
12+
_messageHandlerResolver = messageHandlerResolver;
13+
}
14+
15+
public MessageHandlerDelegate ResolveMessageHandler(Type messageType) => _messageHandlerResolver.ResolveMessageHandler(messageType);
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Autofac;
2+
using System.Collections.Generic;
3+
4+
namespace Xer.Cqrs.Extensions.Autofac
5+
{
6+
/// <summary>
7+
/// Represents an adapter to <see cref="IComponentContext"/>.
8+
/// </summary>
9+
public class ComponentContextAdapter : CommandStack.Resolvers.IContainerAdapter,
10+
EventStack.Resolvers.IContainerAdapter
11+
{
12+
private readonly IComponentContext _context;
13+
14+
public ComponentContextAdapter(IComponentContext context)
15+
{
16+
_context = context;
17+
}
18+
19+
public T Resolve<T>() where T : class => _context.Resolve<T>();
20+
21+
public IEnumerable<T> ResolveMultiple<T>() where T : class => _context.Resolve<IEnumerable<T>>();
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Reflection;
2+
using Xer.Cqrs.Extensions.Autofac;
3+
4+
namespace Autofac
5+
{
6+
public static class ContainerBuilderExtensions
7+
{
8+
public static ContainerBuilder RegisterCqrs(this ContainerBuilder builder, params Assembly[] assemblies)
9+
{
10+
builder.RegisterCqrsCore()
11+
.RegisterEventHandlers(select =>
12+
select.ByInterface(assemblies)
13+
.ByAttribute(assemblies))
14+
.RegisterCommandHandlers(select =>
15+
select.ByInterface(assemblies)
16+
.ByAttribute(assemblies));
17+
18+
return builder;
19+
}
20+
21+
public static ICqrsBuilder RegisterCqrsCore(this ContainerBuilder builder)
22+
{
23+
return new CqrsBuilder(builder);
24+
}
25+
}
26+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Autofac;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Xer.Cqrs.CommandStack;
6+
using Xer.Cqrs.EventStack;
7+
using Xer.Delegator;
8+
using Xer.Delegator.Registrations;
9+
using Xer.Delegator.Resolvers;
10+
11+
namespace Xer.Cqrs.Extensions.Autofac
12+
{
13+
internal class CqrsBuilder : ICqrsBuilder
14+
{
15+
private readonly ContainerBuilder _builder;
16+
private readonly CqrsCommandHandlerSelector _commandHandlerSelector;
17+
private readonly CqrsEventHandlerSelector _eventHandlerSelector;
18+
19+
internal CqrsBuilder(ContainerBuilder builder)
20+
{
21+
_builder = builder;
22+
_commandHandlerSelector = new CqrsCommandHandlerSelector(_builder);
23+
_eventHandlerSelector = new CqrsEventHandlerSelector(_builder);
24+
}
25+
26+
public ICqrsBuilder RegisterCommandHandlers(Action<ICqrsCommandHandlerSelector> selector)
27+
{
28+
if (selector == null)
29+
{
30+
throw new ArgumentNullException(nameof(selector));
31+
}
32+
33+
// Select command handlers to register.
34+
selector(_commandHandlerSelector);
35+
36+
_builder.Register(context =>
37+
{
38+
IMessageHandlerResolver[] handlers = context.Resolve<IEnumerable<CommandHandlerDelegateResolver>>().ToArray();
39+
40+
if (handlers.Any())
41+
{
42+
return handlers.Length > 1
43+
? new CommandDelegator(CompositeMessageHandlerResolver.Compose(handlers))
44+
: new CommandDelegator(handlers[0]);
45+
}
46+
47+
return new CommandDelegator(new SingleMessageHandlerRegistration().BuildMessageHandlerResolver());
48+
}).As<CommandDelegator>().SingleInstance();
49+
50+
return this;
51+
}
52+
53+
public ICqrsBuilder RegisterEventHandlers(Action<ICqrsEventHandlerSelector> selector)
54+
{
55+
if (selector == null)
56+
{
57+
throw new ArgumentNullException(nameof(selector));
58+
}
59+
60+
// Select event handlers to register.
61+
selector(_eventHandlerSelector);
62+
63+
_builder.Register(context =>
64+
{
65+
IMessageHandlerResolver[] handlers = context.Resolve<IEnumerable<EventHandlerDelegateResolver>>().ToArray();
66+
67+
if (handlers.Any())
68+
{
69+
return handlers.Length > 1
70+
? new EventDelegator(CompositeMessageHandlerResolver.Compose(handlers))
71+
: new EventDelegator(handlers[0]);
72+
}
73+
74+
return new EventDelegator(new MultiMessageHandlerRegistration().BuildMessageHandlerResolver());
75+
}).As<EventDelegator>().SingleInstance();
76+
77+
return this;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)