如何在CTOR中使用StructureMap来处理基本类型的情况

时间:2022-06-02 00:24:50

I have following following CTOR for a class:

我跟着以下关于CTOR的课程:

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

   ....
}

I tried the following codes to make DI mapping:

我尝试了以下代码来进行DI映射:

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?

我无法从ObjectFactory.GetInstance ()中获取对象。我想在我的情况下有两个原始参数我不能使用WithCtorArg()来匹配参数。是对的吗?注册我的映射的最佳方法是什么?

2 个解决方案

#1


I started working with StructureMap today and was looking for an answer on * 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:

我今天开始使用StructureMap,当我发现你的问题时,我正在寻找*的答案。你的问题有点老了,如果你没有得到你的答案,这里有:

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");
    }
 ....
}

#2


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

不,你绝对可以: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

注册映射的最佳方法是使用注册表DSL,除了需要从Registry派生并在初始化中配置该注册表外,还需要使用注册表DSL:http://structuremap.sourceforge.net/RegistryDSL.htm

#1


I started working with StructureMap today and was looking for an answer on * 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:

我今天开始使用StructureMap,当我发现你的问题时,我正在寻找*的答案。你的问题有点老了,如果你没有得到你的答案,这里有:

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");
    }
 ....
}

#2


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

不,你绝对可以: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

注册映射的最佳方法是使用注册表DSL,除了需要从Registry派生并在初始化中配置该注册表外,还需要使用注册表DSL:http://structuremap.sourceforge.net/RegistryDSL.htm