将注册名称绑定到structuremap中的组件实例

时间:2012-06-13 19:05:41

标签: structuremap

我即将从Windsor切换到Structuremap,用于包含约100个注册组件(主要是单例)的现有项目。 所有组件都继承自提供日志记录和运行状况跟踪的公共基类,因此包含用于标识组件实例的“Name”属性。

使用Windsor,可以将组件的Name属性设置为用于在IOC容器中注册组件的名称(我们使用了Facility)。

我的问题:结构图是否可以这样?

(我梦想打电话给c.For<IFoo>.Use<Bar>.Named("Doe")神奇地导致某处instanceOfBar.Name = "Doe"

以下是我的尝试:

using System;
using StructureMap;
using StructureMap.Interceptors;
using System.Diagnostics;

namespace ConsoleApplication1
{
    interface IServiceA { }

    interface IServiceB { }

    class Base
    {
        public string Name { get; set; }
    }

    class ComponentA : Base, IServiceA { }

    class ComponentB : Base, IServiceB
    {
        public ComponentB(IServiceA serviceA)
        {
            this.ServiceA = serviceA;
        }

        public IServiceA ServiceA { get; private set; }
    }

    class SetNameInterceptor : TypeInterceptor
    {
        public bool MatchesType(Type type) { return true; }

        public object Process(object target, IContext context)
        {
            // *** Any other way? This does not work...
            string name = context.BuildStack.Current != null ? context.BuildStack.Current.Name : context.RequestedName;
            ((Base)target).Name = name;
            return target;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Container container = new Container(c =>
            {
                c.RegisterInterceptor(new SetNameInterceptor());
                c.For<IServiceA>().Use<ComponentA>().Named("A");
                c.For<IServiceB>().Use<ComponentB>().Named("B");
            });

            var b = container.GetInstance<IServiceB>();

            // both Fail:
            Debug.Assert(((ComponentB)b).Name == "B"); 
            Debug.Assert(((ComponentA)((ComponentB)b).ServiceA).Name == "A");
        }
    }
}

以上显然不起作用,我尝试了几种变化,但没有运气。目标对象的注册名称似乎无法通过IContext始终可访问。

我的第二个最佳方法是定义一个新的“NamedComponent(...)”扩展方法,解析为Named(name).WithProperty(x => x.Name).EqualTo(name),但我想知道是否可以避免将组件注册保持为“类似结构图”尽可能?

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我之前从未使用过WithProperty,但是如果它按照我预期的方式工作应该为你做这个技巧。

我想我会赞成使用EnrichWith。类似的东西:

c.For<IFoo>().Use<Foo>().Named(name).EnrichWith(f => f.Name = name);

EnrichWith对它正在进行的IMO更加明确,并允许您在将其返回给调用者之前调用实例上的任何代码。我喜欢这样你也可以做一个简单的任务。

还有一个更复杂的处理程序,您可以使用EnrichWith来访问请求的上下文 - 这将允许您执行以下操作:

c.For<IFoo>().Use<Foo>().Named(name)
    .EnrichWith((c, i) => {
        i.Name = c.RequestedName;
        return i;
    });

这对你的情况可能有点过分,但是语境意识可能非常有用。

相关问题