私はContext Per Request
パターンを作成するためにEF code first
、MVC
とStructureMap
のチュートリアルで、以下のようにコードを見た:は、StructureMap HttpContextScopedが必要ですか?
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
を必要としています。
あなたはそれだけでContext
ない他人のために、それがReleaseAndDisposeAllHttpScopedObjects()
呼び出すEndRequest
イベントでHttpContextScoped()
を使用して見ることができるように。
1-これは正しいアプローチですか?
2- Service layer Interfaces
の場合はすべてHttpContextScoped()を使用しますか、またはIUnitOfWork
の場合のみではありませんか?例えば
x.For<IFirstEntity>().Use<FirstEntity>();
または
x.For<IFirstEntity>().HttpContextScoped().Use(() => new FirstEntity());
、3- ReleaseAndDisposeAllHttpScopedObjects()
すべてのインスタンスを配置するか、単にContext
を配置しますか?
質問2を更新しました。 –
更新された回答。同じ状態を維持する必要がある場合や、インスタンスの作成に費用がかかり、要求中に状態を共有できる場合を除き、一時オブジェクトを使用してください。 – PHeiberg
ありがとう、私の問題は、これらのインスタンスが処分されるときですか?彼らはEndRequestで処分するのですか? –