views:

254

answers:

1

I'm trying to use StructureMap's InstanceScope.HttpSession feature and I'm running into problems. I have the following method I'm using for testing:

public static class StructureMapTest {
    public static T Get<T>() {
        ObjectFactory.Configure(x => x.AddRegistry(new RepositoryRegistry()));
        return ObjectFactory.GetInstance<T>();
    }
}

My RepositoryRegistry class looks like this:

public class RepositoryRegistry : Registry {
    public RepositoryRegistry() {
        ForRequestedType<IClientRepository>()
            .CacheBy(InstanceScope.HttpSession)
            .TheDefault.Is.OfConcreteType<ClientRepository>();
    }
}

So in my client code (ASP.NET MVC controller) I do something like the following:

public ActionResult InjectionTest() {
    return Content(DataProvider.Clients.CreatedDate.ToString());
}

And every time I call this controller even from the same session, I get a new instance every time. What am I doing wrong here?

A: 

Move the ObjectFactory.Configure to the global asax. You are not supposed to keep reconfiguring structuremap each time an object is requested.

eglasius