StructureMap & # 8594; EnrichWith enriches too much (other instances)

// Enrich with is enriching more than i want

public intefrace ICommand {
   void Execute();
}

// classes

public class A : ICommand {}
public class B : ICommand {}
public class MultiCommand : ICommand {
  public MultiCommand(ICommand[] commands) {}
}

// -- decorators
public DecoratorOne : ICommand {
  public DecoratorOne(Icommand toDecorate) {}
}

public DecoratorTwo : ICommand {
  public DecoratorOne(Icommand toDecorate) {}
}



// what i tried

 ForREquesedType<ICommand>()
    .TheDefaultIsConcreteType<A>
    .EnrichWith(x => new DecoratorOne(x)
    .EnrichWith(y => new DecoratorTwo(y)
    .CacheBy(InstanceScope.Singleton);

 InstanceOf<ICommand>()
    .TheDefault.Is.OfConcreteType<B>
    .EnrichWith(x => new DecoratorOne(x)
    .WithName("secondCommand")

            ForRequestedType<MultiCommand>()
                .TheDefault.Is.OfConcreteType<MultiCommand>()
                .TheArrayOf<ICommand>()
                .Contains(y =>
                              {
                                  y.TheDefault();
                                  y.TheInstanceNamed("secondCommand")
                              })
                .WithName("multi");

**

/// what I want to do

**

What I want is that A is the default. So wherever an ICommand instance is required, it will receive A. MultiCommand will have both A and B and will execute them in a loop.

**

//a problem with

**

B seems to be decorated several times. When I call , I get something in accordance with the new **. I guess it is decorated because of the definition that I have by default. A. How can I avoid this? ObjectFactory.GetNamedInsance<ICommand>("secondCommand")**new DecoratorOne(new DecorateOne(B)).

Also is there a proper way to insert an array into multicommand?

Thanks again, I'm new to the map structure, so any help would be appreciated.

UPDATE

TypeInterceptor, . , , "" . ,

        RegisterInterceptor(new CommandDecoratorInterceptor());

        // this is the default that everyone hooks into
        ForRequestedType<ICOmmand>()
            .TheDefaultIsConcreteType<A>()
            .CacheBy(StructureMap.Attributes.InstanceScope.Singleton);

        InstanceOf<ICommand>()
            .Is.OfConcreteType<B>()
            .WithName("secondCommand");


        ForRequestedType<MultiCommand>()
            .TheDefault.Is.OfConcreteType<MultiCommand>()
            .TheArrayOf<ICommand>()
            .Contains(y =>
                          {
                              y.TheDefault();
                              y.TheInstanceNamed("secondCommand");
                          });

Type , . MultiMonitor ().

;)

+5
1

TheDefaultIsConcreteType . , ForRequestedType(), , ICommands. TheDefault.Is.OfConcreteType.

Enrich, . :

    ForRequestedType<ICommand>()
        .CacheBy(StructureMap.Attributes.InstanceScope.Singleton)
        .TheDefault.Is.OfConcreteType<A>()
        .EnrichWith(x => new DecoratorTwo(new DecoratorOne(x)));
    InstanceOf<ICommand>().Is
        .OfConcreteType<B>()
        .EnrichWith(x => new DecoratorOne(x))
        .WithName("second");
    InstanceOf<ICommand>().Is
        .OfConcreteType<MultiCommand>()
        .TheArrayOf<ICommand>().Contains(y =>
        {
            y.TheDefault();
            y.TheInstanceNamed("second");
        })
        .WithName("multi");
+4

All Articles