C#:包装方法调用的优雅方式

发布于 2024-10-31 07:07:10 字数 1763 浏览 6 评论 0原文

对于相当模糊的标题表示歉意,但我想要实现的目标可能在代码中更好地说明。

我有一个 WCF 客户端。当我调用方法时,我想将每个调用包装在一些错误处理代码中。因此,我没有直接公开这些方法,而是在客户端类上创建了以下辅助函数:

    public T HandleServiceCall<T>(Func<IApplicationService, T> serviceMethod)
    {
        try
        {
            return serviceMethod(decorator);
        }
        [...]
    }

客户端代码如下使用它:

service.HandleServiceCall(channel => channel.Ping("Hello"));

对 Ping 的调用很好地封装在一些逻辑中,该逻辑将尝试处理任何错误。

这非常有效,只是我现在需要知道服务上实际调用了哪些方法。最初,我希望仅使用表达式树检查 Func 但没有走得太远。

最后,我选择了装饰器模式:

    public T HandleServiceCall<T>(Func<IApplicationService, T> serviceMethod)
    {
        var decorator = new ServiceCallDecorator(client.ServiceChannel);
        try
        {
            return serviceMethod(decorator);
        }
        [...]
        finally
        {
            if (decorator.PingWasCalled)
            {
                Console.Writeline("I know that Ping was called")
            }
        }
    }

装饰器本身:

    private class ServiceCallDecorator : IApplicationService
    {
        private readonly IApplicationService service;

        public ServiceCallDecorator(IApplicationService service)
        {
            this.service = service;
            this.PingWasCalled = new Nullable<bool>();
        }

        public bool? PingWasCalled
        {
            get;
            private set;
        }

        public ServiceResponse<bool> Ping(string message)
        {
            PingWasCalled = true;
            return service.Ping(message);
        }
    }

它确实很笨重,而且代码很多。 有更优雅的方法吗?

Apologies for the fairly ambiguous title but what I'm trying to achieve is probably better stated in code.

I have a WCF client. When I'm calling methods I would like to wrap each call in some error handling code. So, instead of exposing the methods directly, I've created the following helper function on the client class:

    public T HandleServiceCall<T>(Func<IApplicationService, T> serviceMethod)
    {
        try
        {
            return serviceMethod(decorator);
        }
        [...]
    }

And the client code uses it like this:

service.HandleServiceCall(channel => channel.Ping("Hello"));

And the call to Ping gets nicely wrapped in some logic that will try to handle any errors.

This works great except that I now have a requirement to know which methods are actually being called on the service. Initially , I was hoping to just inspect the Func<IApplicationService, T> using Expression trees but didn't get very far.

Finally, I settled on a Decorator pattern:

    public T HandleServiceCall<T>(Func<IApplicationService, T> serviceMethod)
    {
        var decorator = new ServiceCallDecorator(client.ServiceChannel);
        try
        {
            return serviceMethod(decorator);
        }
        [...]
        finally
        {
            if (decorator.PingWasCalled)
            {
                Console.Writeline("I know that Ping was called")
            }
        }
    }

And the Decorator itself:

    private class ServiceCallDecorator : IApplicationService
    {
        private readonly IApplicationService service;

        public ServiceCallDecorator(IApplicationService service)
        {
            this.service = service;
            this.PingWasCalled = new Nullable<bool>();
        }

        public bool? PingWasCalled
        {
            get;
            private set;
        }

        public ServiceResponse<bool> Ping(string message)
        {
            PingWasCalled = true;
            return service.Ping(message);
        }
    }

It's really clunky and quite a lot of code. Is there a more elegant way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野心澎湃 2024-11-07 07:07:10

您可以使用表达式,然后检查主体。

像这样的东西

public T HandleServiceCall<T>(Expression<Func<IApplicationService, T>> serviceMethod)     
{         
    try         
    {          
        var func = serviceMethod.Compile();
        string body = serviceMethod.Body.ToString();
        return func(new ConcreteAppService()); 
    }        
    catch(Exception ex)
    {
        ...     
              }
}

You could use an Expression, then inspect the body.

Something like

public T HandleServiceCall<T>(Expression<Func<IApplicationService, T>> serviceMethod)     
{         
    try         
    {          
        var func = serviceMethod.Compile();
        string body = serviceMethod.Body.ToString();
        return func(new ConcreteAppService()); 
    }        
    catch(Exception ex)
    {
        ...     
              }
}
风吹雪碎 2024-11-07 07:07:10

您是否考虑过使用面向方面的方法?听起来正是您所需要的。

包装异常和其他“元方法”功能可以编写为与您的 serviceMethods 所做的“正交”的方面。

关于 AOP 的一些一般信息:维基百科中的 AOP

以及带有容器的潜在解决方案:AOP 与温莎城堡

Have you considered using aspect oriented approach? It sounds like exactly what you need.

Wrapping exceptions and other 'meta method' functionality could be written as aspects 'orthogonal' to what your serviceMethods do.

Some general information on AOP: AOP in wikipedia

And a potential solution with a container: AOP with Windsor Castle

俏︾媚 2024-11-07 07:07:10

下面是一个使用表达式树的简单示例:

public T HandleServiceCall<T>(Expression<Func<T>> serviceMethod)
{
    try
    {
        return serviceMethod();
    }
    finally
    {
        var serviceMethodInfo = ((MethodCallExpression)serviceMethod.Body).Method;
        Console.WriteLine("The '{0}' service method was called", serviceMethodInfo.Name);
    }
}

请注意,此示例假定serviceMethod 表达式始终包含方法调用。

相关资源:

Here's a quick example using an expression tree:

public T HandleServiceCall<T>(Expression<Func<T>> serviceMethod)
{
    try
    {
        return serviceMethod();
    }
    finally
    {
        var serviceMethodInfo = ((MethodCallExpression)serviceMethod.Body).Method;
        Console.WriteLine("The '{0}' service method was called", serviceMethodInfo.Name);
    }
}

Note that this example assumes that the serviceMethod expression always contains a method invocation.

Related resources:

淡看悲欢离合 2024-11-07 07:07:10

是的,我相信你的代码已经煮过头了。

关于包装代码以进行常见的安全处置代理,请查看此处 一个很好的实现。使用它很简单:

using (var client = new Proxy().Wrap()) {
 client.BaseObject.SomeMethod();
}

现在您还需要访问方法名称 - 只需使用 Environment.StackTrace 即可。您需要在 Marc Gravell 的 Wrap 中添加向上遍历堆栈的功能。

Yes, I believe your code is overcooked.

In terms of Wrapping your code for a common safe disposing of proxies, look here for a nice implementation. Using it is easy:

using (var client = new Proxy().Wrap()) {
 client.BaseObject.SomeMethod();
}

Now you also need to access method name - for that simply use Environment.StackTrace. You need to add walking up the stack in Marc Gravell's Wrap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文