I'm using MS Unity and 'Registration by convention' (auto-registration) to register all the classes in a namespace. The code (seen below) works at it should and returns an expected result
var container = new UnityContainer();
container.RegisterTypes( AllClasses.FromLoadedAssemblies().Where(
t => t.Namespace == "DependencyInjectionExample.Test"),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.ContainerControlled);
Result
Container has 4 registrations
- IUnityContainer '[default]' Container
- TestClass1 '[default]' ContainerControlled
- TestClass2 '[default]' ContainerControlled
- TestClass3 '[default]' ContainerControlled
My problem is that I can't figure out how to resolve them. I've tried with
var allRegistered = container.ResolveAll<ITestable>();
But it doesn't return anything (all the testclasses implement ITestable). When I try
var singleRegistered = container.Resolve<ITestable>();
I get a 'ResolutionFailedException' - "The type ITestable does not have an accessible constructor". I've read that it is because that the registered types are not named, but that is not possible when using auto-registration.
What should I do to resolve the registered types?
EDIT
namespace DependencyInjectionExample.Test
{
public interface ITestable
{
string SaySomething();
}
}
One of the three test classes. They all do the same thing.
namespace DependencyInjectionExample.Test
{
public class TestClass1 : ITestable
{
public TestClass1() { }
public string SaySomething()
{
return "TestClass1 hello";
}
}
}