NInject DI とリポジトリ パターンを使用して、MVC3 アプリケーションを作成しています。Ninject は、リポジトリがリクエストごとの有効期間を持つように設定されています。
次のコードを使用して、コンテキスト オブジェクトを Http Request オブジェクトに入れています。
public static MessengerEntities GetContext()
{
if (!HttpContext.Current.Items.Contains("_db_context"))
{
HttpContext.Current.Items.Add("_db_context", new MessengerEntities());
}
return (MessengerEntities)HttpContext.Current.Items["_db_context"];
}
次に、各リポジトリはこのプロシージャを呼び出して、既存または新しいコンテキスト オブジェクトを取得します。
public class TestRepository : ITestRepository
{
private MessengerEntities context = ContextHelper.GetContext();
#region ITestRepository Members
private string _testProperty = "blah";
public string testProperty
{
get
{
_testProperty = context.UserLogins.Where(n => n.inactive == null || !n.inactive.Value).ToList().Count.ToString();
return _testProperty;
}
set
{
_testProperty = value;
}
}
#endregion
}
(後で、一般的な IRepository パターンを使用する予定ですが、今のところ、このテスト リポジトリを使用しています。)
私の質問は、Request オブジェクトが破棄されると、Items コレクションのコンテキスト オブジェクトも破棄されるのでしょうか? つまり、そのコレクションに格納される可能性のある各オブジェクトで Dispose を呼び出すでしょうか?
ここでこの問題について多くの議論があることは知っていますが、それらはすべて私のものとまったく同じではないシナリオを含んでいるように見えるので、答えを推測するのはちょっと難しいです.