Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac lazy TypedParameter

Tags:

autofac

In Autofac is it possible to make TypedParameter lazy? Even more, I need access to container when injecting parameter. Code could look like this:

builder.RegisterType<RootService>()
    .WithParameter(TypedParameter.From(c => c.Resolve<IChildService>(key)));

Update

Based on Nick's answer I've created the following helper method:

public static class TypedResolvedParameter 
{
    public static ResolvedParameter From<T>(Func<IComponentContext, T> factory)
    {
        return new ResolvedParameter(
            (pi, c) => pi.ParameterType == typeof(T),
            (pi, c) => factory(c));
    }
}
like image 692
Konstantin Spirin Avatar asked Mar 16 '11 04:03

Konstantin Spirin


1 Answers

You're looking for ResolvedParameter, also available as an overload to WithParameter():

builder.RegisterType<RootService>()
  .WithParameter((pi, c) => pi.ParameterType == typeof(IChildService),
                 (pi, c) => c.ResolveNamed<IChildService>(key));
like image 163
Nicholas Blumhardt Avatar answered Jan 03 '23 00:01

Nicholas Blumhardt