EF code firstのチュートリアルで以下のようなコードを見て、パターンMVCをStructureMap作成しました。Context Per Request
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および...すべてIunitOfWorkのコンストラクターが必要です。
ご覧のとおり、他のユーザーには使用HttpContextScoped()せContextず、EndRequest呼び出した場合に使用しますReleaseAndDisposeAllHttpScopedObjects()。
1-これは正しいアプローチですか?
2- HttpContextScoped()を他のすべてに使用しますか、それともService layer Interfaces単に使用しIUnitOfWorkますか?例えば
x.For<IFirstEntity>().Use<FirstEntity>();
また
x.For<IFirstEntity>().HttpContextScoped().Use(() => new FirstEntity());
3-ReleaseAndDisposeAllHttpScopedObjects()すべてのインスタンスを破棄しますか、それとも単に破棄しContextますか?