城堡温莎配置工厂

时间:2012-10-15 12:30:16

标签: castle-windsor

我有一个场景,我不知道如何最好地解决城堡问题。我正在尝试根据从外部系统检索到的ID来解析类型。棘手的部分是多个id与相同的具体类型相关。

到目前为止,我已经提出了这个问题。这只是感觉有点不对劲。我不喜欢我需要致电ResolveAll的事实。

    [TestFixture]
public class TestWindsor
{

    [Test]
    public void AddGreenResolverToContainer_ShouldResolveCorrectTypeAndTypeId()
    {
        //---------------Set up test pack-------------------
        IWindsorContainer container = new WindsorContainer();
        //---------------Assert Precondition----------------

        //---------------Execute Test ----------------------
        container.Register(Component.For<IResolver>()
                               .ImplementedBy<TestOneResolver>()
                               .DependsOn(new {typeId = "1"}));
        //---------------Test Result -----------------------
        var actual = container.Resolve<IResolver>();
        Assert.IsInstanceOf<TestOneResolver>(actual);
        Assert.AreEqual("1", actual.typeId);
    }

    [Test]
    public void AddTwoNamedGreenResolverToContainer_ShouldResolveTwoTypes()
    {
        //---------------Set up test pack-------------------
        IWindsorContainer container = new WindsorContainer();
        //---------------Assert Precondition----------------

        //---------------Execute Test ----------------------
        container.Register(Component.For<IResolver>()
                               .ImplementedBy<TestOneResolver>()
                               .DependsOn(new {typeId = "1"}).Named("tageventstatusresolver"));
        container.Register(Component.For<IResolver>()
                               .ImplementedBy<TestOneResolver>()
                               .DependsOn(new {typeId = "2"}).Named("second"));            
        container.Register(Component.For<IResolver>()
                               .ImplementedBy<TestTwoResolver>()
                               .DependsOn(new {typeId = "1122"}).Named("redFirst"));
        container.Register(Component.For<IResolverFactory>()
                                    .ImplementedBy<ResolverFactory>()
                                    .DependsOn(new { resolvers=container.ResolveAll<IResolver>()}));


        //---------------Test Result -----------------------
        var actual = container.Resolve<IResolverFactory>();
        var resolver = actual.Create("1");
        Assert.IsInstanceOf<TestOneResolver>(resolver);
    }

}


public interface IResolverFactory
{
    IResolver Create(string typeId);
}

public class ResolverFactory : IResolverFactory
{
    private readonly IResolver[] _resolvers;

    public ResolverFactory(IResolver[] resolvers)
    {
        _resolvers = resolvers;
    }

    public IResolver Create(string typeId)
    {
        return _resolvers.Where(resolver => resolver.typeId == typeId).FirstOrDefault();
    }
}

public enum ResolutionStatus
{
    Red,
    Green,
    Amber
}

public interface IResolver
{
    string typeId { get; }
    ResolutionStatus Resolve();
}

public class TestOneResolver : IResolver
{
    public TestOneResolver(string typeId)
    {
        this.TypeId = typeId;
    }

    public string TypeId { get; private set; }

    public ResolutionStatus Resolve()
    {
        return ResolutionStatus.Green;
    }
}

public class TestTwoResolver : IResolver
{
    public TestTwoResolver(string typeId)
    {
        TypeId = typeId;
    }

    public string TypeId { get; private set; }

    public ResolutionStatus Resolve()
    {
        return ResolutionStatus.Red;
    }
}

有没有人建议更好的方法呢?

1 个答案:

答案 0 :(得分:0)

将组件名称设置为typeId值,然后按this approach创建按组件名称创建的类型化工厂。