Fork me on GitHub

WhatDoIHave() Edit on GitHub


The IContainer.WhatDoIHave() method can give you a quick textual report of the current configuration of a running Container:


var container = new Container();
var report = container.WhatDoIHave();

Debug.WriteLine(report);

If you're familiar with WhatDoIHave() from earlier versions of StructureMap, the usage has been enhanced for 3.0 to allow you to filter the results for easier usage. The format was also tweaked extensively to (hopefully) improve the usability of this feature.

Enough talk, say you have a Container with this configuration:


var container = new Container(x =>
{
    x.For<IEngine>().Use<Hemi>().Named("The Hemi");

    x.For<IEngine>().Add<VEight>().Singleton().Named("V8");
    x.For<IEngine>().Add<FourFiftyFour>().AlwaysUnique();
    x.For<IEngine>().Add<StraightSix>().LifecycleIs<ThreadLocalStorageLifecycle>();

    x.For<IEngine>().Add(() => new Rotary()).Named("Rotary");
    x.For<IEngine>().Add(c => c.GetInstance<PluginElectric>());

    x.For<IEngine>().Add(new InlineFour());

    x.For<IEngine>().UseIfNone<VTwelve>();
    x.For<IEngine>().MissingNamedInstanceIs.ConstructedBy(c => new NamedEngine(c.RequestedName));
});

If you were to run the code below against this Container:


Debug.WriteLine(container.WhatDoIHave());

you would get the output shown in this gist.

If you're curious, all the raw code for this example is in here.

Filtering WhatDoIHave()

Filtering the WhatDoIHave() results can be done in these ways:


var container = new Container();

// Filter by the Assembly of the Plugin Type
var byAssembly = container.WhatDoIHave(assembly: typeof(IWidget).GetAssembly());

// Only report on the specified Plugin Type
var byPluginType = container.WhatDoIHave(typeof(IWidget));

// Filter to Plugin Type's in the named namespace
// The 'IsInNamespace' test will include child namespaces
var byNamespace = container.WhatDoIHave(@namespace: "StructureMap.Testing.Widget");

// Filter by a case insensitive string.Contains() match
// against the Plugin Type name
var byType = container.WhatDoIHave(typeName: "Widget");