ravendbストレージのバックエンドとして使用しています。パターンを使用してunit of workいるため、セッションを開いてアクションを実行し、結果を保存してセッションを閉じる必要があります。私は自分のコードをきれいに保ちたいので、各アクションで明示的にセッションの開始と終了を呼び出さないようにしたいので、このコードを次のようにOnActionExecutingandOnActionExecutedメソッドに配置します。
#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しかし、名前で指定されたメソッドでこれを取得するにはどうすればよいですか?