In the EF code first , MVC and StructureMap tutorial, I saw the code as shown below to create the Context Per Request template:
protected void Application_Start() { ... initStructureMap(); } private static void initStructureMap() { ObjectFactory.Initialize(x => { x.For<IUnitOfWork>().HttpContextScoped().Use(() => new Context()); x.For<IFirstEntity>().Use<FirstEntity>(); x.For<ISecondEntity>().Use<SecondEntity>(); x.For<IThirdEntity>().Use<ThirdEntity>(); }); ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory()); } protected void Application_EndRequest(object sender, EventArgs e) { ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects(); } public class StructureMapControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { return ObjectFactory.GetInstance(controllerType) as Controller; } }
FirstEntity , SecondEntity and ... IunitOfWork needs IunitOfWork in its constructor.
as you can see, it just uses HttpContextScoped() for Context not others, and in the EndRequest event it calls ReleaseAndDisposeAllHttpScopedObjects() .
1- is this the right approach?
2- use HttpContextScoped () for all other Service layer Interfaces or not only for IunitOfWork ? eg
x.For<IFirstEntity>().Use<FirstEntity>();
or
x.For<IFirstEntity>().HttpContextScoped().Use(() => new FirstEntity());
3- ReleaseAndDisposeAllHttpScopedObjects() uses all instances or just has Context ?
asp.net-mvc-3 entity-framework structuremap
mohsen dorparasti
source share