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:
- is there a built-in function that does the same?
- if not, can my code be improved performance wise?
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!