ravendb
ストレージのバックエンドとして使用しています。パターンを使用してunit of work
いるため、セッションを開いてアクションを実行し、結果を保存してセッションを閉じる必要があります。私は自分のコードをきれいに保ちたいので、各アクションで明示的にセッションの開始と終了を呼び出さないようにしたいので、このコードを次のようにOnActionExecuting
andOnActionExecuted
メソッドに配置します。
#region RavenDB's specifics
public IDocumentSession DocumentSession { get; set; }
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.IsChildAction)
{
return;
}
this.DocumentSession = Storage.Instance.OpenSession();
base.OnActionExecuting(filterContext);
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.IsChildAction)
{
return;
}
if (this.DocumentSession != null && filterContext.Exception == null)
{
this.DocumentSession.SaveChanges();
}
this.DocumentSession.Dispose();
base.OnActionExecuted(filterContext);
}
#endregion
ただし、一部のアクションには接続が必要であり、接続が必要ありravendb
ません。そこで、カスタム属性を作成し、それを使用して DocumentSession を開く必要があるメソッドをマークすることにしました。次に例を示します。
//
// GET: /Create
[DataAccess]
public ActionResult Create()
{
return View();
}
そして、私は立ち往生しました。OnActionExecuted
私の計画は、メソッドでアクションの属性を取得し、[DataAccess]
存在する場合は open にすることDocumentSession
でした。
では、ステートメントOnActionExecuted
を介してアクション名 (メソッド名) を取得できます。filterContext.ActionDescriptor.ActionName
しかし、リフレクションを使用して特定のクラスのメソッドの属性を取得するにはどうすればよいですか?
Attribute.GetCustomAttributes
それが呼び出しである可能性があることがわかりましたが、最も近いMemberInfo
のはメソッドのオブジェクトが必要です。MemberInfo
しかし、名前で指定されたメソッドでこれを取得するにはどうすればよいですか?