tags:

views:

6

answers:

0

I'm trying to use setter injection with StructureMap. Yes, I want setter, not constructor injection. I'd like to use it on a custom attribute class, where constructor injection would mess up the [Attribute(constructor params)] syntax.

I put together a sample console application to figure out how to do this, but it's not working. What am I doing wrong?

public interface IInjectable
{
    string Property { get; set; }
}

public class Injectable : IInjectable
{
    #region IInjectable Members

    public string Property { get; set; }

    #endregion
}

public class InjectMe
{
    [SetterProperty]
    public IInjectable WantInjection { get; set; }
}

internal class Program
{
    private static void Main(string[] args)
    {
        ObjectFactory.Initialize(e =>
                                     {
                                         var r = new Registry();
                                         r.Scan(a =>
                                                    {
                                                        a.TheCallingAssembly();
                                                        a.WithDefaultConventions();
                                                    });
                                         e.AddRegistry(r);
                                         e.FillAllPropertiesOfType<IInjectable>().Use(
                                             new Injectable());
                                         e.For<InjectMe>().Use(new InjectMe());
                                     });
        var i = ObjectFactory.GetInstance<InjectMe>();
        Console.WriteLine(i.WantInjection == null ? "fail" : "win");
        Console.ReadKey();
 }
}

I'm always getting "fail".