How can I find out what was done to resolve Type?

Jan 17, 2011 at 12:36 PM


is there a built in way to find out what additional steps Unity performs when it resolves a Type?

E.g if I have the following definitions:

interface IFoo { }

interface IBar { }

class Foo: IFoo
{ MyClass(IBar bar) { //... } }

When I resolve IFoo Unity would have to resolve IBar for use as constructor parameter. Is there a way to find out that Unity had to resolve IBar?

I only need information about resolution of parameters for constructor injection.


Jan 17, 2011 at 9:26 PM

That information gets baked into the generated methods, it's not saved explicitly anywhere. If I may ask, why do you want this information? It may inform future decisions about the evolution of the container if I can get more info about the scenario you're trying to achieve.


Jan 18, 2011 at 6:44 AM

At times I really need to know what the container did in detail. I need to know what was registered (ok, I can find that out already), when a type was resolved and what mappings where used. Which resolves used interception? It would make a very usefull tool for diagnostics.

Another use case is for a feature on my wish list for [Unity vNext]. It is not too difficult to add contextual binding to Unity (after you finally manage to put the right pieces together). But one addition I would like to make is to enable resolution based on types that are resolved. If I resolve IFoo it maps to class Foo, which needs a constructor parameter of Type IBar. Now I want to see that Foo was that last step in the resolution process and I can use that information to answer the question "to which implementation shall I map IBar?".

Jan 18, 2011 at 6:09 PM

So this is about diagnostics?

We do track fairly detailed information about what the container is doing during a resolve so we can produce useful error messages. You could add an additional strategy at the front of the chain and sock the information away (in a policy, probably) to get back afterwards. You're not going to be able to capture this before resolving a type, as the entire graph isn't calculated until then.

I'm loathe to build this in full time into the container, as it will require potentially large increased memory usage and not actually be used in that majority of usages.


Jan 19, 2011 at 9:19 AM

OK, if that information is already there I won't have to collect it myself :-)

Is there one single location where I can get hold of it?

Jan 19, 2011 at 7:32 PM

The information on what the container is currently doing is stored in the CurrentOperation field of the build context. A couple things to remember though:

1) This field is of type object - unless you go through everywhere that sets this property and finds the type, pretty much all you can do on it is call ToString on it.

2) The entire tree is not stored, only the current path the container is working on. Once a dependency is done, the information in CurrentOperation is overwritten.

So you'll need to add a strategy at the front of the build chain to collect this information and assemble it. So there's still some work on your part to get this.