CraftyFella
CraftyFella

Reputation: 7568

StructureMap property setter injection with open generic using FillAllPropertiesOfType

I have the following class:

public abstract class Query<TResult>
{
    protected abstract TResult Result();

    public TResult Execute()
    {
        return Result();
    }

    public ISession Session { get; set; }
}

I wanted to use property injection to populate the Session. Which would mean anything inheriting from Query would be able to query using the Session.

Anyway.. It's always null :(

I have the following StructureMap Registry code:

public class MyStructureMapRegistry : Registry
{
    public MiStructureMapRegistry()
    {
        Scan(scanner =>
                 {
                    scanner.TheCallingAssembly();
                    scanner.WithDefaultConventions();
                    For<ISession>().HttpContextScoped().Use(x => x.GetInstance<ISessionFactory>().OpenSession());
                    FillAllPropertiesOfType<ISession>().Use(x => x.GetInstance<ISession>());
                 });
    }
}

Can anyone suggest what I'm doing wrong?

Upvotes: 1

Views: 836

Answers (1)

Jay Otterbein
Jay Otterbein

Reputation: 968

You need to call the IContainer.BuildUp() method to initialize properties on the object.

Example:

public void PerformQuery<TResult>()
{
    var query = ObjectFactory.GetInstance<Query<TResult>>();
    ObjectFactory.BuildUp(query);
    return query.Execute();
}

Upvotes: 1

Related Questions