Configuradores de autowire usando StructureMap

Digamos que tengo lo siguiente:

public interface IDoStuff 

public class DoerOfStuff : IDoStuff

public class RandomClass
    public IDoStuff Doer {get; set; }

Asumiendo RandomClass has only a default constructor (and we'd like to keep it that way) my questions are:

  1. How can I bootstrap StructureMap so that the Doer in RandomClass get autowired to DoerOfStuff?
  2. How can I make it more generic, such that any class that has an IDoStuff property gets autowired to DoerOfStuff?
  3. ¿Cómo puedo hacer DoerOfStuff a Singleton?

Gracias de antemano!

Note: I assume this is possible with StructureMap so ideally the answer will show that. If for some reason you know it isn't though, please feel free to show how you'd do it with a different container.


Here is how I was able to get it working:

ObjectFactory.Initialize(x =>
    x.SetAllProperties(policy => policy.TypeMatches(type => type == typeof(IDoStuff)));
    // this works too
    // x.FillAllPropertiesOfType<IDoStuff>();

In either case though, this only worked if I also added the following:

public RandomClass()

I am still curious if I can skip the BuildUp() aunque de alguna manera.

preguntado el 09 de marzo de 12 a las 13:03

1 Respuestas

new Container(x => {
 x. FillAllPropertiesOfType<IDoStuff>();

respondido 09 mar '12, 14:03

Since Doer is a property, isn't it also required to run IContainer.BuildUp() on the RandomClass object after it's been created to initialize the properties? - jay otterbein

For some reason this isn't working for me, I get a null reference. If I do an ObjectFactory.GetInstance<IDoStuff> it works though, so I am not sure what's wrong. - Sean Gough

The only way I was able to get this to work was to add the ObjectFactory.BuildUp(this); to the RandomClass (see my edit). If you add that to your answer or show me how I don't need it I will gladly accept your answer. - Sean Gough

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.