4

Note 1: I want to make this clear: I am not trying to lazy load dependencies or inject lazy types.

Most (all?) IoC containers require metadata to be registered with the container to describe how some type should be resolved when asked; this includes the implementation of an interface, the lifetime of the object, etc. Over the past couple of years, the fluent/convention based APIs have been added to most (all?) IoC containers which reduces a lot of the noise when providing this metadata to the container.

With ninject, I am using a conventions based approach to register my metadata, but I do it on demand, not upfront. This yields huge performance savings in an integration test scenario where you are creating ~10 objects; you lose the overhead of having to register the entire application's dependencies every test.

Note 2: Please do not tell me that you should not use an IoC container in an integration test.

There is some pressure to move to using Microsoft components when available, so Unity is on my horizon. I have the conventions based registration working, but I cannot figure out how to "discover" this metadata on demand/lazily like I could in Ninject.

My first attempt to implement this was using a wrapper around the UnityContainer instance, and upon calling GetInstance(type) on the wrapper, I can query the container to see if the type is registered. If not, I can invoke my conventions, discover the metadata, and register the type. This approach only goes one level deep however; if that type is injected with more dependencies, each dependency does not get fed back into the GetInstance(type) method, and thus, my dilemma.

The question: How can I lazily/on-demand add type registrations to Unity?

Source would be nice, but a pointer to a hook in the container would be just as good. I tried inheritance but there is nothing to override like in Ninject.

2
  • Can you give an example, how you do this in Ninject? I might be able to figure out how to do the same in Unity. However intuitively, I do agree with the opinion that in most cases there is no benefit with lazy registration. In Unity registering a type mapping is just creating half a dozen very small objects and putting them in certain collections. It can't be too slow can it? Commented Mar 11, 2011 at 11:34
  • Deferred resolution is now is now supported by Unity 3. See msdn.microsoft.com/en-us/library/… . Commented Oct 20, 2013 at 19:38

1 Answer 1

5

First of all - every problem with Unity can, and should be solved by avoiding using it.

If that's not an option, I think there's no option in Unity to register stuff on demand.

As a sidenote, I would still not default to trying to register stuff lazily. The fact that it yields unacceptable performance with one container doesn't mean that would be the case with another. Have you measured it first?

I use container (Windsor) a lot in tests and I never found up-front registration to be a problem. More over, (I know that those are tests so different rules apply) I think not registering stuff upfront is wrong. Most containers I'm aware of even if allow this, are still heavily optimised for up-front registration so your performance gain could be zero, or even you could find yourself seeing worse numbers than doing it the right way and registering upfront.

Sign up to request clarification or add additional context in comments.

4 Comments

Whenever you say something like "every problem with Unity can, and should be solved by avoiding using it", it's good to also provide a reason why you think this way. Otherwise the response may come across as baseless.
I don't think that was related to the question. And anyone who tries Unity, can google or even search through SO can quickly learn that it's not baseless.
could you provide some links please
Paraphrasing Raymond Chen: Good advice comes with a rationale, so you can tell when it's still good advice (blogs.msdn.microsoft.com/oldnewthing/20091104-00/?p=16153)

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.