-
Notifications
You must be signed in to change notification settings - Fork 278
/
OpenGenericDecorator_question.cs
63 lines (52 loc) · 1.64 KB
/
OpenGenericDecorator_question.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
using NUnit.Framework;
using Shouldly;
using StructureMap.Building.Interception;
using StructureMap.Graph;
namespace StructureMap.Testing.Bugs
{
// This came from SO: http://stackoverflow.com/questions/32722306/decorator-interception-with-open-generics-in-structuremap-3
[TestFixture]
public class OpenGenericDecorator_question
{
[Test]
public void applies_the_decorator()
{
var container = new Container(_ =>
{
_.Policies.Interceptors(new CommandHandlerLoggingDecoration());
_.Scan(scan =>
{
scan.TheCallingAssembly();
scan.ConnectImplementationsToTypesClosing(typeof (ICommandHandler<>));
});
});
container.GetInstance<ICommandHandler<Command1>>()
.ShouldBeOfType<CommandHandlerLoggingDecorator<Command1>>()
.Inner.ShouldBeOfType<Command1Handler>();
}
}
public interface ICommandHandler<T>
{
}
public class CommandHandlerLoggingDecoration : DecoratorPolicy
{
public CommandHandlerLoggingDecoration()
: base(typeof (ICommandHandler<>), typeof (CommandHandlerLoggingDecorator<>), type => true)
{
}
}
public class CommandHandlerLoggingDecorator<T> : ICommandHandler<T>
{
public ICommandHandler<T> Inner { get; set; }
public CommandHandlerLoggingDecorator(ICommandHandler<T> inner)
{
Inner = inner;
}
}
public class Command1
{
}
public class Command1Handler : ICommandHandler<Command1>
{
}
}