我如何使用简单的面向方面的概念来处理没有postharp的异常处理? [英] How can i use simple aspect oriented concept to handle exception handling without postsharp?

查看:61
本文介绍了我如何使用简单的面向方面的概念来处理没有postharp的异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AOP处理控制台应用程序中的错误异常. (这不是MVC,我不是使用属性花瓶编程来处理mvc中的错误,但这是控制台应用程序)下面的代码:(如果发生错误,则应在我的属性端代码中抛出一个错误)

i want to use AOP to handle my error exception in Console application. ( it is not MVC i used attribute vase programing to handle errors in mvc but this is console app) My code below: ( if error occurs ,it should throw an error yo my attribute side code )

 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class HandleError : Attribute
{
     public HandleError(string description)
    {
        try
        {
            this.Description = description;
        }
        catch (Exception)
        {

            throw;
        }

    }
    public string Description { get; set; }


}

这将从我的方法中调用:

this will call from my method :

   [HandleError("Error occurs here")]
    public static void MyMethod(string data)
    {
        throw new Exception();

实际上;我想使用AOP处理我的方法内的异常.如果发生错误,我必须调用属性.但是如何? (请不要提供后期处理,它需要金钱.但是我也对开源开放)为什么不容易,我不明白.

Actually; i want to use AOP to handle exceptions inside my method. i have to call attributes if it error occurs. But How? ( please don't offer postsharp, it needs money. but i am open for opensource also)By the way; why it is not easy ,i don't understand.

推荐答案

基本上,PostSharp的作用是在编译时将代码编织到程序集中,这些代码在用属性标记的方法之前和之后运行.从性能的角度来看,这非常好,因为没有使用在运行时动态创建的代码.

Basically, what PostSharp does is to weave code into your assembly at compile time that is run before and after the methods that are marked with the attributes. This is very good from a performance point of view because there is no use of code that is created dynamically at runtime.

其他一些AOP框架(或IoC容器)提供了生成动态代理的选项,该代理包含在运行时拦截对方法的调用的代码.

Some other AOP frameworks (or IoC containers) offer the option to generate dynamic proxies that contain code that intercepts the calls to the methods at runtime.

要么使用这些框架之一(查找IoC和拦截),要么自己实现可比较的功能.基本上,您要做的就是将要拦截的代码移到一个类中,并将方法标记为virtual.在运行时,您可以使用从类继承并覆盖方法的动态创建的类来装饰类的实例,以便在调用该方法之前和之后运行其他代码.

Either you use one of those frameworks (look for IoC and interception) or you implement a comparable functionality by yourself. Basically what you have to do is to move the code you want to intercept into a class and mark the methods as virtual. At runtime, you decorate the instance of the class with a dynamically created class that inherits from your class and overrides the methods so that the additional code is run before and after the call to the method.

但是,可能有一种更简单的方法可以满足控制台应用程序的需求.除了用属性标记方法之外,您还可以创建一些帮助程序函数,其中包含要在方法之前和之后运行的代码:

However, there might be a simpler approach that fits the needs of a console application. Instead of marking the methods with an attribute, you could also create some helper functions that contain the code that you want to run before and after the method:

void Main()
{
    int value = GetValue(123);
    DoSomething(value);
}

void DoSomething(int myParam)
{
    RunAndLogError(() => {
        // Place code here
        Console.WriteLine(myParam);
        });
}

int GetValue(int myParam)
{
    return RunAndLogError(() => {
        // Place code here
        return myParam * 2;});
}

void RunAndLogError(Action act)
{
    try
    {
        act();
    }
    catch(Exception ex)
    {
        // Log error
        throw;
    }
}

T RunAndLogError<T>(Func<T> fct)
{
    try
    {
        return fct();
    }
    catch(Exception ex)
    {
        // Log error
        throw;
    }
}

如您所见,RunAndLogError有两个重载,一个重载用于void方法,另一个重载用于返回值的方法.

As you can see, there are two overloads of RunAndLogError, one for void methods, the other one for methods that return a value.

另一个选择是为此目的使用全局异常处理程序;有关详细信息,请参见以下答案: .NET控制台应用程序中的全局异常处理程序

Another option is to use a global exception handler for this purpose; see this answer for details: .NET Global exception handler in console application

这篇关于我如何使用简单的面向方面的概念来处理没有postharp的异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆