Using Unity, I'd like to automatically register all interface/class combinations in an assembly based on the following convention:
INameOfObject > NameOfObject
StructureMap does that when the default conventions are enabled.
I wrote the following method for it:
private static IUnityContainer RegisterITypesOf(this IUnityContainer container, string assemblyName)
{
Assembly.Load(assemblyName)
.GetTypes()
.Where(t => t.GetInterfaces().Any(i => i.Name == "I" + t.Name))
.ForEach(t => container.RegisterType(t.GetInterface("I" + t.Name, false), t));
return container;
}
My question is:
Unity 3.5.140.0 has built in functionality to register interfaces with similar names as the class that uses it.
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default);
}
I wanted the same as you; convention based configuration ala Structuremap, and went ahead and created a library for it. You can download it on NuGet, and read some documentation on my github page
Hope this helps!
Unity does not have support for conventions. But the TecX project on codeplex contains an enhanced configuration engine for Unity that is based on StructureMap's configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With