Ninject: passing the client assembly as an argument to the constructor and specifying the desired implementation from the client using attributes

I am new to Ninject and Dependency Injection in general, so please excuse my noobility here. =)

I have several interface implementations IConfigthat scan the assembly and its dependencies for types that implement the interface IConfigOption. Each implementation IConfigextracts their values ​​from different sources.

These implementations take the root assembly, where to start scanning as an argument to the constructor, and I'm trying to find a binding that introduces these values. So far I am thinking of something in the lines:

Bind<IConfig>().To<Config>().WithConstructorArgument("rootAssembly", target);

My problem is that I cannot find a way to get a reference to the assembly targetwhere the class will be introduced Config. Without DI, I would use Assembly.GetCallingAssembly(), but in this context, it gives the assembly where the binding is, not the target.

Also, I need a way to specify the intended implementation from the class / member that will receive the injection , so for example, client class A requests from a developer Configwho uses RoleEnvironmentand client class B requests Configwhich one uses ConfigurationManager. Here is an example to clarify the intended use:

public class Client
{
    [UseApplicationConfig]
    public IHelper WithAppConfig { get; set; }

    [UseRoleEnvironmentConfig]
    public IHelper WithRoleEnvironmentConfig { get; set; }
}

public class Helper : IHelper
{
    public Helper(IConfig config)
    {
    }
}

I get the feeling that I'm either looking at it from the wrong angle, or I'm missing the obvious, but I'm not sure what I need to look for. What would be the best way to do this?

Many thanks.

EDIT: .

+3
1

, , WithConstructorArgument(string, Func<IContext, object>). IContext ( , , , , ).

, :

//get the full name of the requested object
Bind<IFoo>()
    .To<Foo>()
    .WithConstructorArgument("bar",
                             c => c.Request.FullName);

, . , , , , , , Ninject . , , DI?

WhenInjectedInto IHelper, . . : https://github.com/ninject/ninject/wiki/Contextual-Binding

- , :

Bind<IWarrior>()
    .To<Samurai>()
    .When(request => request.Target.Type.Namespace.StartsWith("Samurais.Climbing"));

, , - WhenInjectedInto , " ", .

+1

All Articles