Last active
January 3, 2016 09:29
-
-
Save gabrieljoelc/8443330 to your computer and use it in GitHub Desktop.
Structure Map scanner convention for registering concrete types that implement multiple of the same interfaces but with varying generic parameter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HandleDomainEventsScanner : IRegistrationConvention | |
{ | |
public void Process(Type type, Registry registry) | |
{ | |
foreach(var interfaceType in type.GetInterfaces() | |
// see https://gist.github.com/gabrieljoelc/5706069 for IsGenericTypeAssignableFrom() extension method | |
.Where(t => typeof(IHandleDomainEvents<>).IsGenericTypeAssignableFrom(t))) | |
registry.AddType(interfaceType, type)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyDualPurposeHandlerClass : IHandleDomainEvents<Foo>, | |
IHandleDomainEvents<Bar> | |
{ | |
public void Handle(Foo @event) | |
{} | |
public void Handle(Bar @event) | |
{} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyStructureMapRegistry : Registry | |
{ | |
public MyStructureMapRegistry() | |
{ | |
Scan(scanner => | |
{ | |
scanner.AssemblyContainingType<MyStructureMapRegistry>(); | |
scanner.Convention<HandleDomainEventsScanner>(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment