ASP.NET MVC プロジェクトを開発しており、厳密に型指定されたセッション オブジェクトを使用したいと考えています。このオブジェクトを公開するために、次の Controller 派生クラスを実装しました。
public class StrongController<_T> : Controller
where _T : new()
{
public _T SessionObject
{
get
{
if (Session[typeof(_T).FullName] == null)
{
_T newsession = new _T();
Session[typeof(_T).FullName] = newsession;
return newsession;
}
else
return (_T)Session[typeof(_T).FullName];
}
}
}
これにより、コントローラーごとにセッション オブジェクトを定義できます。これは、コントローラー分離の概念に沿ったものです。より良い/より「正しい」方法、おそらくMicrosoftによって公式にサポートされているものはありますか?