2

A. Simple question.

I have 3 instances of "Repository" that depends on the "Configuration".

class Manager{ 
  public Manager(Configuration conf){
...

Resolving strategies for 3 instances :

container.RegisterType<Repository>("ForService1");
container.RegisterType<Repository>("ForService2");
container.RegisterType<Repository>("ForService3");

(three because of they have additional parameters that are not included into sample, parameters that always differ)

and the "default" configuration

container.RegisterType<Configuration>(new InjectionConstructor(new object[] { false, true }));

I want to provide the possibility to redefine the Configuration in the config file for one and only one instance.

    <register type="Configuration" 
              name="**ForService2**">
      <constructor>
        <param name = "useOptimization1" value="True"/>
        <param name = "useOptimization2" value="True"/>
      </constructor>
    </register>

And want to avoid to have triple configuration in the extension like:

container.RegisterType<Configuration>("ForService1", new InjectionConstructor(new object[] { false, true }));
container.RegisterType<Configuration>("ForService1", new InjectionConstructor(new object[] { false, true }));
container.RegisterType<Configuration>("ForService2", new InjectionConstructor(new object[] { false, true }));

Is this possible? How?

B. Complex Question.

I get the feeling that IoC tool is not about configuration. Trying to configure infrastructure throw IoC config file - is yet another antipattern. Or shorter : "Configuration Information Data is not a Dependency". Am I right?

Let observe EntLib's listner configuration. Really, is EntLib's log listener configuration a "late binding" or a "configuration way"? First, this is the "late binding" (since we point the type name - what assembly to loas and object to create) and also this is the "configuration" (we have custom configuration section). I feel that most of IoC followers will choose the container's enabled "configuration", am I right? But EntLib guys choose the configuration section. May be this custom config section for log listener's configuration will change container's configuration and that how "listener configuration" should be related to container's configuration?

SheetJS
  • 22,470
  • 12
  • 65
  • 75
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142

2 Answers2

1

Answer A

Use a container with support for Convention over Configuration. You can see a comparison chart here.

Answer B

Correct, IoC shouldn't be about configuration. Instead, prefer code as configuration, preferably in the form of encoded conventions.

Community
  • 1
  • 1
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • Thank you for your answer, Mark. But I need to add that after reading of your answer in the "prefer code as configuration" link I still do not share your assurance that there are clear bold line between configuration and "late binding". I've added sample to the question. – Roman Pokrovskij Oct 17 '11 at 00:00
1

I want to confirm that the best way is to have container configured from the code and even not just from the code, but from wise reach code. Ultimate sample is the EntLib's configuration from "constructors lambda expressions library":

http://msdn.microsoft.com/en-us/magazine/ee335709.aspx

yield return new TypeRegistration<Database>(
       () => new SqlDatabase(
           ConnectionString,
           Container.Resolved<IDataInstrumentationProvider>(Name)))
       {
           Name = Name,
           Lifetime = TypeRegistrationLifetime.Transient
       };

such constructions will be later interpreted to build container configuration!

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142