views:

341

answers:

2

I have following following CTOR for a class:

public class Log : ILog {
   ...
   public Log (string file, string flag) { .... }

   ....
}

I tried the following codes to make DI mapping:

public MyStructureMap {

    public void static InitializeMapping() {
       StructureMap.DSL.Registiry.ForRequestedType<ILog>().TheDefault.Is
          .OfConcreteType<Log>().WithCtorArg("file").EqualTo(@"C:\tmp\log.txt");
       StructureMap.DSL.Registiry.ForRequestedType<ILog>().TheDefault.Is
          .OfConcreteType<Log>().WithCtorArg("flag").EqualTo(@"debug");
    }
 ....
}

I could not get the object from ObjectFactory.GetInstance<ILog>() to work. I guess that in my case with two primitive parameters I cannot use WithCtorArg() to match parameters. Is that right? What is the best way to register my mapping?

+2  A: 

No, you definitely can: http://structuremap.sourceforge.net/InstanceExpression.htm#section5

The best way to register your mapping is with the registry DSL, which you are sort of using there, except you need to derive from Registry and configure that registry in your initialization: http://structuremap.sourceforge.net/RegistryDSL.htm

Matt Hinze
+3  A: 

I started working with StructureMap today and was looking for an answer on StackOverflow for something else when I spotted your question. Your question is a little old, but in case you didn't get your answer, here goes:

You can use multiple primitive parameters. You just have to change your syntax to take advantage of the fluent interface:

public MyStructureMap {

    public void static InitializeMapping() {
       StructureMap.DSL.Registiry.ForRequestedType<ILog>().TheDefault.Is.OfConcreteType<Log>()
          .WithCtorArg("file").EqualTo(@"C:\tmp\log.txt");
          .WithCtorArg("flag").EqualTo(@"debug");
    }
 ....
}
dariom