【问题标题】:Configuring profiles with StructureMap使用 StructureMap 配置配置文件
【发布时间】:2009-02-19 18:16:02
【问题描述】:

重要;我真的在这里寻找StructureMap 的答案。请不要说如何使用 Windsor、Spring、Unity 或 the others 中的任何一个。

我正在为 IoC 使用 StructureMap - 基本上我的目标是拥有一个定义核心类型的“默认”配置文件,以及一些覆盖/扩展它的命名配置文件。我认为配置文件可以做到这一点,但我根本无法通过 xml 或代码 API 让它工作。特别是,如果我尝试为配置文件加载容器:

container = new Container();
container.SetDefaultsToProfile(profile);

然后我得到“找不到请求的配置文件 {name}”,尽管我在初始化中明确调用了 CreateProfile(使用该名称)。

我是不是找错树了?

(也发到user-group


我理想中想要的是能够定义 standard (/default) 类型,然后对于一系列不同的命名配置, 覆盖一些设置 - 即如果我有

  • 全局:IFoo => Foo, IBar => Bar
  • configA:(无变化)
  • configB: IFoo => SpecialFoo

我相信这可以映射到 2 个使用命名配置文件加载的容器。 目的是如果我向任一容器询问IBar,我会得到一个 Bar - 但 configA 返回一个 Foo(对于 IFoo),其中 configB 返回一个 SpecialFoo

有人可以告诉我如何配置它吗? xml 或 代码很好......我只是想让它工作。我所需要的只是接口到- 具体类型映射(没有特殊的配置/属性设置)。

【问题讨论】:

    标签: .net configuration structuremap


    【解决方案1】:

    诀窍是确保每个配置文件都至少作为其中定义的规则。如果您不指定规则 (configA),它将不会创建/查看配置文件。

    鉴于这些类:

    public interface IFoo { string SayHello(); }
    public class Foo : IFoo { public string SayHello() { return "Hello"; } }
    public class SpecialFoo : IFoo { public string SayHello() { return "Hello Special"; } }
    
    public interface IBar { }
    public class Bar : IBar { }
    
    public interface IDummy { }
    public class Dummy : IDummy{ }
    

    您可以定义此注册表:

    public class MyRegistry : Registry
    {
        protected override void configure()
        {
            ForRequestedType<IBar>().TheDefault.Is.OfConcreteType<Bar>();
            ForRequestedType<IFoo>().TheDefault.Is.OfConcreteType<Foo>();
            CreateProfileNotEmpty("configA");
            CreateProfileNotEmpty("configB")
                .For<IFoo>().UseConcreteType<SpecialFoo>();
        }
        StructureMap.Configuration.DSL.Expressions.ProfileExpression CreateProfileNotEmpty(string profile)
        {
            return CreateProfile(profile)
                .For<IDummy>().UseConcreteType<Dummy>();
        }
    }
    

    它适用于这些测试:

    [TestMethod]
    public void TestMethod1()
    {
        var container = new Container(new MyRegistry());
        Assert.IsNotNull(container.GetInstance<IBar>());
        Assert.AreEqual("Hello", container.GetInstance<IFoo>().SayHello());
    
        container.SetDefaultsToProfile("configB");
        Assert.IsNotNull(container.GetInstance<IBar>());
        Assert.AreEqual("Hello Special", container.GetInstance<IFoo>().SayHello());
    
        container.SetDefaultsToProfile("configA");
        Assert.IsNotNull(container.GetInstance<IBar>());
        Assert.AreEqual("Hello", container.GetInstance<IFoo>().SayHello());
    }
    

    如果将 CreateProfileNotEmpty 替换为简单的 CreateProfile,它将在将默认设置为 configA 的行上失败。

    【讨论】:

    • 有趣。我明天试试。认为它是+1(可能是“胜利”)-我现在只是没有选票;-p
    【解决方案2】:

    观看此视频,他展示了另一种执行“默认”配置文件的方法,并使用其他命名配置文件作为变体。

    http://www.dimecasts.net/Casts/CastDetails/135

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多