I really like StructureMap as an IOC-framework especially the convention based registration. Now I try to do the following: I want to add all types that implement a specific interface when the class has a default (no parameters) constructor. And the types must be created using this constructor.
This is what I have untill now, which registers the correct types, but how do I specify that the default constructor should be used when creating an instance.
public class MyRegistry : Registry
{
public MyRegistry()
{
Scan(
x =>
{
x.AssemblyContainingType<IUseCase>();
x.Exclude(t => !HasDefaultConstructor(t));
x.AddAllTypesOf<IUseCase>();
});
}
private static bool HasDefaultConstructor(Type type)
{
var _constructors = type.GetConstructors();
return _constructors.Any(c => IsDefaultConstructor(c));
}
private static bool IsDefaultConstructor(ConstructorInfo constructor)
{
return !constructor.GetParameters().Any();
}
}