开发者

How to add an ActionFilter to a non MVC project?

开发者 https://www.devze.com 2023-03-05 12:53 出处:网络
I have several projects written in f/x4.0 that are not MVC.No web involved.In fact, one is a Windows Service, another is a set of Libraries used for either Win Forms or Command Line processing.Now, in

I have several projects written in f/x4.0 that are not MVC. No web involved. In fact, one is a Windows Service, another is a set of Libraries used for either Win Forms or Command Line processing. Now, in MVC3 you can do something like:

using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RivWorks.FeedHandler.Library
{
    class AttributeLogsRequest : ActionFilterAttribute, IActionFilter
    {
        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
        {
            // do something here...
        }

        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            // do something here...
        }
    }
}

Now all you have to do is add an ActionFilter to a Method or Class definition and the above 2 calls will happen automatically:

[AttributeLogsRequest]
public class MyController
{
    ...
}

My question is: Is there anything else other than ActionFilterAttribute, IActionFilter I can use to create non-MVC dependant Filters for my code? ActionFilterAttribute, IActio开发者_运维知识库nFilter provides information that is very MVC centric like ControllerName, Action, HttpContext, etc. THere must be a simpler way to create a simple filter for cross-cutting concerns.

TIA


Look like you need some AOP library to do this.

There are a lot of them out there. You can try 1 of the followings:

PostSharp

Spring AOP

Unity Policy Injection

0

精彩评论

暂无评论...
验证码 换一张
取 消