0

I am using structure map and setting up a service based on the profile string passed. I have a list of objects. List of IProcessors that I need to inject to the RecognizerSaga to process against each of these processors.

Say, bootstrapping code looks something like this:

x.CreateProfile("A1", p => {
        p.Type<IProcessor>().Is.OfConcreteType<TestAProcessor>();
        p.Type<IProcessor>().Is.OfConcreteType<TestBProcessor>();
        p.Type<IProcessor>().Is.OfConcreteType<TestCProcessor>();
    }

x.CreateProfile("B2", p => {
        p.Type<IProcessor>().Is.OfConcreteType<TestAProcessor>();
        p.Type<IProcessor>().Is.OfConcreteType<TestBProcessor>();
    });

When I get these processors I try to build it using ObjectFactory.GetAllInstances<IProductProcessor> and it returns me all 5 of them though I have setup the default profile as A1. Is there a way to get all instances within a profile. If I say, A1, get me only 3 of them?

For work around I build a factory class which builds object for me, but I would prefer having structuremap all the way.

4

1 に答える 1

0

残念ながら、これは現在 StructureMap では不可能です。

プロファイルの考え方は、プラグイン タイプのデフォルトの具体的なタイプを指定することです。同じインターフェイスのプロファイルのデフォルト タイプを設定するたびに、デフォルトの具象タイプを再定義します。

探していることを行う別の方法は、型の配列を構成して、それ自体がプロファイルの既定の型として設定されている具体的な型を渡すことです。

これが機能することを証明するテストの例を次に示します。

    public interface IProcessor { }
public class ProcessorA : IProcessor { }
public class ProcessorB : IProcessor { }
public class ProcessorC : IProcessor { }

public class ProcessorGroup
{
    public IProcessor[] Processors { get; private set; }

    public ProcessorGroup(IProcessor[] processors)
    {
        Processors = processors;
    }
}

public class ProcessorRegistry : Registry
{
    public ProcessorRegistry()
    {
        CreateProfile("a", profile =>
        {
            profile.For<ProcessorGroup>().UseNamedInstance("a");
        });

        CreateProfile("b", profile =>
        {
            profile.For<ProcessorGroup>().UseNamedInstance("b");
        });

        ForConcreteType<ProcessorGroup>()
            .Configure.WithName("a")
            .TheArrayOf<IProcessor>().Contains(a =>
            {
                a.OfConcreteType<ProcessorA>();
            });

        ForConcreteType<ProcessorGroup>()
            .Configure.WithName("b")
            .TheArrayOf<IProcessor>().Contains(a =>
            {
                a.OfConcreteType<ProcessorB>();
                a.OfConcreteType<ProcessorC>();
            });
    }
}

[TestFixture]
public class test
{
    [Test]
    public void processor_group_a()
    {
        var container = new Container(new ProcessorRegistry());

        container.SetDefaultsToProfile("a");
        var processorGroup = container.GetInstance<ProcessorGroup>();

        processorGroup.Processors.Length.ShouldEqual(1);
    }   

    [Test]
    public void processor_group_b()
    {
        var container = new Container(new ProcessorRegistry());

        container.SetDefaultsToProfile("b");
        var processorGroup = container.GetInstance<ProcessorGroup>();

        processorGroup.Processors.Length.ShouldEqual(2);
    }   
}
于 2009-09-21T17:14:04.757 に答える