拦截从同一个对象的方法调用的方法 [英] Intercepting method called from a method of the same object

查看:24
本文介绍了拦截从同一个对象的方法调用的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下:

////<摘要>///一个业务逻辑类.///</总结>公共类 BusinessClassWithInterceptor : BusinessClass, IBusinessClass{///<总结>///初始化 <see cref="BusinessClassWithoutInterceptor"/> 的新实例班级.///</总结>///<param name="logger">记录器.</param>public BusinessClassWithInterceptor(Logger logger):基地(记录器){}///<总结>///显示所有奶牛.///</总结>public void DisplayAllCows(){this.Logger.Write("显示所有奶牛:");var repository = new CowRepository();foreach (CowEntity 母牛在 repository.GetAllCows()){this.Logger.Write(" " + cow);}}///<总结>///插入一个诺曼底.///</总结>public void InsertNormande(int id, string name){this.DisplayAllCows();var repository = new CowRepository();repository.InsertCow(new CowEntity { Id = id, Name = name, Origin = CowOrigin.Normandie });}}

用城堡温莎,这个类被配置为用这个拦截器拦截:

////<摘要>///用于记录业务方法的拦截器.///</总结>公共类 BusinessLogInterceptor : IInterceptor{///<总结>///拦截指定的调用.///</总结>///<param name="invocation">调用.</param>公共无效拦截(IInvocation调用){Logger logger = ((IBusinessClass)invocation.InvocationTarget).Logger;var 参数 = new StringBuilder();ParameterInfo[] methodParameters = invocation.Method.GetParameters();for (int index = 0; index < methodParameters.Length; index++){parameters.AppendFormat("{0} = {1}", methodParameters[index].Name, invocation.Arguments[index]);if (index < methodParameters.Length - 1){parameters.Append(", ");}}logger.Format("调用 {0}( {1} )", invocation.Method.Name, parameters.ToString());调用.继续();logger.Format("退出{0}", invocation.Method.Name);}}

该问题发生在调用 InsertNormande 期间.对InsertNormande的调用被很好的拦截了,但是在InsertNormande对DisplayAllCows的调用没有被拦截...

这真的让我很困扰.

这种场景下有没有办法实现拦截?

解决方案

我不认为有一种简单的方法可以做到...无法拦截类内部的方法调用,因为它们没有't 通过代理.

您可以通过其他方式实现所有方法的日志记录,例如像 PostSharp 之类的 AOP 框架>

Here's the situation:

/// <summary>
/// A business logic class.
/// </summary>
public class BusinessClassWithInterceptor : BusinessClass, IBusinessClass
{
    /// <summary>
    /// Initializes a new instance of the <see cref="BusinessClassWithoutInterceptor"/> class.
    /// </summary>
    /// <param name="logger">The logger.</param>
    public BusinessClassWithInterceptor(Logger logger)
        : base(logger)
    {
    }

    /// <summary>
    /// Displays all cows.
    /// </summary>
    public void DisplayAllCows()
    {
        this.Logger.Write("Displaying all cows:");
        var repository = new CowRepository();
        foreach (CowEntity cow in repository.GetAllCows())
        {
            this.Logger.Write("    " + cow);
        }
    }

    /// <summary>
    /// Inserts a normande.
    /// </summary>
    public void InsertNormande(int id, string name)
    {
        this.DisplayAllCows();

        var repository = new CowRepository();
        repository.InsertCow(new CowEntity { Id = id, Name = name, Origin = CowOrigin.Normandie });
    }
}

With castle windsor, this class is configured to be intercepted with this interceptor:

/// <summary>
/// Interceptor for logging business methods.
/// </summary>
public class BusinessLogInterceptor : IInterceptor
{
    /// <summary>
    /// Intercepts the specified invocation.
    /// </summary>
    /// <param name="invocation">The invocation.</param>
    public void Intercept(IInvocation invocation)
    {
        Logger logger = ((IBusinessClass)invocation.InvocationTarget).Logger;

        var parameters = new StringBuilder();
        ParameterInfo[] methodParameters = invocation.Method.GetParameters();
        for (int index = 0; index < methodParameters.Length; index++)
        {
            parameters.AppendFormat("{0} = {1}", methodParameters[index].Name, invocation.Arguments[index]);
            if (index < methodParameters.Length - 1)
            {
                parameters.Append(", ");
            }
        }

        logger.Format("Calling {0}( {1} )", invocation.Method.Name, parameters.ToString());
        invocation.Proceed();
        logger.Format("Exiting {0}", invocation.Method.Name);
    }
}

The issue takes place during the call to InsertNormande. The call to InsertNormande is well intercepted, but the call to DisplayAllCows in InsertNormande is not intercepted...

It really bothers me.

Is there a way to achieve interception in this scenario ?

解决方案

I don't think there's an easy way of doing it... method calls that are internal to the class can't be intercepted, since they don't go through the proxy.

You could achieve logging of all methods by other means, such as an AOP framework like PostSharp

这篇关于拦截从同一个对象的方法调用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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