2

I have a MCV3 app that uses Ninject as a DI container.

I have one object that has 2 constructors (each with 1 parameter)

public MyObject(IDependencyOne dep){}
public MyObject(IDependencyTwo dep){}

How can I make Ninject choose one of the two constructors based on some boolean value read from web.config file at runtime?

Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65

2 Answers2

0

From here, it says : Ninject doesn't have XML configuration.

Free yourself from XML

Most other .NET dependency injection frameworks are designed around the use of XML to declare type bindings. Rather than forcing you to write cumbersome and error-prone text, Ninject arms you with a fluent interface, which lets you connect the pieces of your application using full-fledged code. This means you can take advantage of the features of the IDE and compiler, like code completion and type-safety.

There's been a general trend (among the folks I know or follow, anyway) to moving IoC bindings into code and out of XML. Mostly b/c you gain intellisense and runtime feedback of screw-ups.

I haven't used it but there are release candidates for Ninject.Extensions.Xml which allows you to setup your mappings in xml. However I would generally shy away from doing it this way. There's also Ninject.Extensions.Conventions which might be what you're after.

Source :

Ninject and configuration

Ninject and XML configuration Binding

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • 1
    This is not what I meant, NInject is fluently configured but what bindings to configure needs to be decided dynamically at runtime. – Liviu Mandras Oct 26 '12 at 09:26
0

You'd use the constructor selection syntax with Conditional Bindings - see this related question/answer and the relevant wiki section on Contextual Binding.

Community
  • 1
  • 1
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249