1

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?

Thanks

Dave

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
CraftyFella
  • 7,520
  • 7
  • 47
  • 61

1 Answers1

1

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();
}
Jay Otterbein
  • 948
  • 5
  • 10
  • Hi reading the docs. it's seems BuildUp is used for setting properties of an already created object. Like in your example.. I want to auto set properties, as it describes here: http://structuremap.net/structuremap/ConstructorAndSetterInjection.htm#section7 – CraftyFella Mar 07 '12 at 08:31
  • Jay, your answer was 100% correct. Turns out I was newing up the Query. I.e. By passing StructureMap.. so how can it set the ISession. BuildUp therefor is the correct solution. Thanks :) – CraftyFella Mar 07 '12 at 08:56