0

I've just come across an issue recently where I want new types injected into the requested type every time it is resolved.

The current code I have to register the type is

container.RegisterType<IFirstT, FirstT>();

container.RegisterType<ISecondT, SecondT>();

container.RegisterType<IInjectableT, InjectableT>()
    .Configure<InjectedMembers>()
    .ConfigureInjectionFor<InjectableT>(
          new InjectionConstructor(
                  container.Resolve<IFirstT>(),
                  container.Resolve<ISecondT>(),
           )
    );

I've now come to realise that the same injection constructor is being used every time I resolve the IInjectableT.

Is it possible that the InjectionConstructor will create new Dependencies everytime with unity?

I realise that I can just resolve the dependencies inside of the constructor of InjectableT and achieve the same thing, however I was intending for the IOC to controll this type of behaviour and choose if a new instance should be injected or an existing one passed to it.

3
  • Just found that if I use typeOf(T) instead of container.Resolve<T>() in the InjectionConstructor I get the result I'm after Commented Mar 4, 2011 at 0:40
  • 5
    If you already resolved the issue, you should either: 1. provide a real answer and accept it so that it's easier to see that this question has been answered. 2. delete the question. Commented Mar 4, 2011 at 8:23
  • While it resolved my issue I wanted the best solution known. I wasn't sure that my answer was the most correct one or just a work around. Commented Mar 24, 2011 at 23:34

1 Answer 1

3

You should use ResolvedParameter:

container.RegisterType<IInjectableT, InjectableT>(
          new InjectionConstructor(
                  new ResolvedParameter<IFirstT>(),
                  new ResolvedParameter<ISecondT>()
           )
    );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.