Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using names to discriminate instances using IoC

I'm trying out Ninject and I'm modifying to code I wrote with Structure Map to see how easy it is. In this base code I have a graph of objects which have different configurations via the Structure Map registries and the one to use is chosen at runtime via a value in the database (in this case to pull back a wcf service body with some objects injected). So for example (using the Structure Map code):

Registry 1 sets up all defaults for the IBusinessContext, IRules, and ILogger types. This is just adding the types GenericContext/Logger/Rules along side the interfaces with no other specialisation.

public GenericRegistry()
    {
        // Set up some generic bindings here
        For<ILogger>().Use<Loggers.GenericLogger>();
        For<IBusinessRule>().Use<Rules.StandardRule>();
        For<IBusinessContext>().Use<Contexts.GenericBusinessContext>();
        For<ILoggerContext>().Use<Loggers.GenericLoggerContext>();
    }

Registry 2 sets up IBusinessContext to use the SpecialisedContext class and tells the ctor to use SpecializedLogger. The instance for IBusinessContext is named "SpecializedContext".

public SpecializedRegistry()
    {
        // Old style syntax as it affects the default for IBusinessContext
        // Perhaps a hint at what I'm doing?
        InstanceOf<IBusinessContext>().Is.OfConcreteType<Contexts.SpecializedBusinessContext>().Named(SpecializedInstanceName).Ctor<ILogger>().Is<Loggers.SpecialisedLogger>();
    }

This all works as expected in Structure Map (depending on old or new syntax).

However, when I've been using Ninject I hit a problem with expecting the unnamed instance to be default (not how Ninject works, I get that). This led to some research which all suggested that using named instances is A Really Bad Idea. I understand that there are better ways to do this using auto registration or attributes to set a name or requesting a certain type, but in the system I'm describing there needs to be a way at run time to figure out what configuration to ask for at the top of the tree (and let the IoC framework figure out the rest based on registered types or rules).

So...am I just using the IoC concept wrong here by expecting to ask for my top level object by name or is there generally a better way to do what I'm trying to do? Should I be using something like MEF instead and treating this all like plug-ins?

I stress I'm not using this like a dumb factory and asking at each level of the code for a instance of type x from the container, it is the initiating action only.

Thanks in advance for your time and help :)

like image 334
NoodleAwa Avatar asked Feb 14 '12 14:02

NoodleAwa


1 Answers

There is nothing all that wrong with setting up ninject bindings by name, if that is the only way to achieve what it is you need IMO.

So the basic syntax is:

Bind<IBusinessContext>().To<ConcreteBusinessContext>().Named("XYZ");

Or if you need a specific calling class to get a different binding then you can try:

Bind<IIBusinessContext>().To<SomeOtherConcreteBusinessContext>().WhenInjectedInto<TypeOfCallingClass>();

However, if the calling class (I'm talking about the class that has the IBusinessContext in its ctor) supplies a configuration value which determines which concrete type to load, then you will need to use a delegate:

Bind<Func<string, IBusinessContext>>().ToMethod(ctx => str => DetermineWhichConcreteTypeToLoad(ctx, str));

//messy sudo code
static DetermineWhichConcreteTypeToLoad(IContext ctx, string str)
{
    if(str == "somevalue"){
        return ctx.Kernel.Get<ConcreteType1>();
    else
        return ctx.Kernel.Get<ConcreteType2>();
}

and your calling class will look something like:

class DoStuff
{
    Func<string, IBusinessContext>> contextFunc;

    DoStuff(Func<string, IBusinessContext>> contextFunc)
    {
        this.contextFunc = contextFunc;
    }

    void SomeMethod()
    {
        var configuredValue = GetConfiguredValueSomehow();
        var context = contextFunc(configuredValue); //<-- this passes your config value back to ninject in the ToMethod() lambda
        //do something with context
    }
}

In that example there is no need for named instances, as you have a method which loads the specific concrete type, however you can still use named instances if you want to do something like this:

Bind<IBusinessContext>().To<ConcreteBusinessContext>().Named("config1");
Bind<IBusinessContext>().To<SomeOtherBusinessContext>().Named("config2");

Bind<Func<string, IBusinessContext>>().ToMethod(ctx => str => ctx.Kernel.Get<IBusinessContext>().Named(str));

class DoStuff
{
    Func<string, IBusinessContext>> contextFunc;

    DoStuff(Func<string, IBusinessContext>> contextFunc)
    {
        this.contextFunc = contextFunc;
    }

    void SomeMethod()
    {
        var configuredValue = "config1";
        var context = contextFunc(configuredValue); //<-- this will passthrough "config1" to the above ToMethod() method and ask for a IBusinessContext named "config1"

    }
}

EDIT: I forgot to mention that if your config value doesnt have to come from the calling code, then this makes things much easier. Your code can instead look something like:

// this method can just be a global method in you app somewhere
static string GetConfigValue()
{
    //something like
    return AppSetting.Get("config");
}

Bind<IBusinessContext>().To<ConcreteBusinessContext>().When(r => GetConfigValue() == "config1");
Bind<IBusinessContext>().To<SomeOtherBusinessContext>().When(r => GetConfigValue() == "config2");

class DoStuff
{
    IBusinessContext context;

    DoStuff(BusinessContext context)
    {
        this.context = context;
    }

    void SomeMethod()
    {
        //use the context value as you normally would
    }
}

You can be creative and rather than use magic strings, your config method can load up an enum and your When() method can test for equality against an enum instead of a string, but you get the idea. This is known as contextual binding in ninject, and I can tell you as a once avid user of SM, this is much much more powerful than anything SM had. Checkout the rest of the When() methods and see what you can do.

like image 147
AaronHS Avatar answered Oct 23 '22 12:10

AaronHS