-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
127 lines (93 loc) · 4.66 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using Autofac; //Version="6.4.0"
using NUnit.Framework;
using System.Reflection;
[TestFixture]
public class AutofacTest
{
[Test]
public void ResolveAllCommandHandlers_IsSuccessful()
{
//Arrange
var builder = new ContainerBuilder();
// this works as well instead of the manual registrations below
//builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
// .AsClosedTypesOf(typeof(IHandler<>))
// .AsImplementedInterfaces();
builder.RegisterType(typeof(CommandHandler1))
.As(typeof(ICommandHandler<Command>))
.As(typeof(IHandler<Command>));
builder.RegisterType(typeof(CommandHandler2))
.As(typeof(ICommandHandler<Command>))
.As(typeof(IHandler<Command>));
var container = builder.Build();
//Act
var commandHandlers = ((IEnumerable<IHandler<Command>>)container.Resolve(typeof(IEnumerable<IHandler<Command>>))).ToList();
//Assert
Assert.AreEqual(commandHandlers[0].GetType(), typeof(CommandHandler1));
Assert.AreEqual(commandHandlers[1].GetType(), typeof(CommandHandler2));
}
[Test]
public void ResolveAllDecoratedCommandHandlers_Manual_IsSucessful()
{
//Arrange
var builder = new ContainerBuilder();
builder.RegisterType(typeof(CommandHandler1))
.As(typeof(ICommandHandler<Command>))
.As(typeof(IHandler<Command>));
builder.RegisterType(typeof(CommandHandler2))
.As(typeof(ICommandHandler<Command>))
.As(typeof(IHandler<Command>));
builder.RegisterGenericDecorator(
typeof(CommandHandlerDecorator<>),
typeof(IHandler<>),
context => context.ImplementationType.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICommandHandler<>))
);
var container = builder.Build();
//Act
var commandHandlers = ((IEnumerable<IHandler<Command>>)container.Resolve(typeof(IEnumerable<IHandler<Command>>))).ToList();
//Assert
Assert.AreEqual(((CommandHandlerDecorator<Command>)commandHandlers[0]).Decorated.GetType(), typeof(CommandHandler1)); //fails, decorated is typeof(CommandHandler2)
Assert.AreEqual(((CommandHandlerDecorator<Command>)commandHandlers[1]).Decorated.GetType(), typeof(CommandHandler2));
}
[Test]
public void ResolveAllDecoratedCommandHandlers_Scanning_IsSucessful()
{
//Arrange
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AsClosedTypesOf(typeof(IHandler<>))
.AsImplementedInterfaces();
builder.RegisterGenericDecorator(
typeof(CommandHandlerDecorator<>),
typeof(IHandler<>),
context => context.ImplementationType.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICommandHandler<>))
);
var container = builder.Build();
//Act
var commandHandlers = ((IEnumerable<IHandler<Command>>)container.Resolve(typeof(IEnumerable<IHandler<Command>>))).ToList();
//Assert
Assert.AreEqual(((CommandHandlerDecorator<Command>)commandHandlers[0]).Decorated.GetType(), typeof(CommandHandler1)); //fails, decorated is typeof(CommandHandler2)
Assert.AreEqual(((CommandHandlerDecorator<Command>)commandHandlers[1]).Decorated.GetType(), typeof(CommandHandler2));
}
}
interface IRequest { }
interface IHandler<in TRequest> where TRequest : IRequest { public void Handle(TRequest request); }
interface ICommand: IRequest { }
interface ICommandHandler<in TCommand>: IHandler<TCommand> where TCommand: ICommand { }
interface IQuery : IRequest { }
interface IQueryHandler<in TQuery>: IHandler<TQuery> where TQuery: IQuery { }
class Command : ICommand { }
class CommandHandler1 : ICommandHandler<Command> { public void Handle(Command command) => Console.WriteLine("CommandHandler1"); }
class CommandHandler2 : ICommandHandler<Command> { public void Handle(Command command) => Console.WriteLine("CommandHandler2"); }
class Query: IQuery { }
class QueryHandler1 : IQueryHandler<Query> { public void Handle(Query query) => Console.WriteLine("QueryHandler1"); }
class CommandHandlerDecorator<TCommand> : ICommandHandler<TCommand> where TCommand : ICommand
{
public ICommandHandler<TCommand> Decorated { get; }
public CommandHandlerDecorator(ICommandHandler<TCommand> decorated) => Decorated = decorated;
public void Handle(TCommand request)
{
Console.WriteLine($"Command Decorator for {Decorated.GetType().FullName}");
Decorated.Handle(request);
}
}