I have a type that receives another type in its constructor, which usually is the type of the object that creates it, e.g.:
public class Logger {
public Logger(Type parent) { ... }
}
I would like to instruct Unity to resolve Logger
passing as the argument to its constructor the type of the object that requires it. Something like:
// ... would be some directive to tell Unity to use the type that
/// depends on Logger
container.RegisterType<Logger>(new InjectionConstructor(...));
So that when I try to resolve MyService
:
public MyService {
public MyService(Logger logger) { ... }
}
it will return:
var logger = new Logger(typeof(MyService));
return new MyService(logger);
Is it possible? Is there another way of doing it?
Actually, you can do this:
internal class Program
{
static void Main( string[] args )
{
var container = new UnityContainer();
container.RegisterType<IInterface, Implementation>( new MyInjectionConstructor() );
// this instance will get a logger constructed with loggedType == typeof( Implementation )
var instance = container.Resolve<IInterface>();
}
}
internal class MyInjectionConstructor : InjectionMember
{
public override void AddPolicies( Type serviceType, Type implementationType, string name, IPolicyList policies )
{
policies.Set<IConstructorSelectorPolicy>( new MyConstructorSelectorPolicy(), new NamedTypeBuildKey( implementationType, name ) );
}
}
internal class MyConstructorSelectorPolicy : DefaultUnityConstructorSelectorPolicy
{
protected override IDependencyResolverPolicy CreateResolver( ParameterInfo parameter )
{
if( parameter.ParameterType == typeof( ILogger ) )
{
return new LiteralValueDependencyResolverPolicy( new Logger( parameter.Member.DeclaringType ) );
}
return base.CreateResolver( parameter );
}
}
internal interface ILogger
{
}
internal class Logger : ILogger
{
public Logger( Type loggedType )
{
}
}
internal interface IInterface
{
}
internal class Implementation : IInterface
{
public Implementation( ILogger logger )
{
}
}
This is proof of concept code only, and might need to be refined a bit before production use...