温莎容器中的城堡动态代理

发布于 2024-08-23 07:07:48 字数 1985 浏览 12 评论 0原文

我有一点问题。我在温莎城堡 IOC 容器中工作。现在我想做的只是搞乱一些 AOP 原则,我特别想做的是基于方法名称执行一些日志记录。我一直在研究拦截器,目前我正在使用作为类实现的 IInterceptor 接口来使用方面执行此日志记录。问题是,如果我想对特定方法执行日志记录,那么它会变得混乱,因为我需要在实现的方面中放入一些逻辑来检查方法名称等...

我已经读到,您可以使用以下命令来完成所有这些操作动态代理以及 IInterceptorSelector 接口和 IProxyGenerationHook 接口。我在网上看到了一些这样的例子,但我很困惑这一切如何适合 Windsor 容器。我的意思是我正在使用 Windsor 容器,它在我的代码中实际上是对 IWindsorContainer 接口的引用来创建我的所有对象。我的所有配置都是通过代码而不是 XML 完成的。

首先,除了我目前正在做的方式之外,还有人知道在温莎容器中执行特定于方法的 AOP 的方法吗?

其次,我如何在温莎容器中使用动态代理?

下面我添加了创建代理并注册我的类的代码 拦截器

ProxyGenerator _generator = new ProxyGenerator(new PersistentProxyBuilder());
IInterceptorSelector _selector = new CheckLoggingSelector();
var loggingAspect = new LoggingAspect();
var options = new ProxyGenerationOptions(new LoggingProxyGenerationHook()) 
                  { Selector = _selector };
var proxy = _generator.CreateClassProxy(typeof(TestClass), options, loggingAspect);
TestClass testProxy = proxy as TestClass;

windsorContainer.Register(
Component.For<LoggingAspect>(),
Component.For<CheckLoggingAspect>(),
Component.For<ExceptionCatchAspect>(),
Component.For<ITestClass>()
    .ImplementedBy<TestClass>()
    .Named("ATestClass")
    .Parameters(Parameter.ForKey("Name").Eq("Testing"))
    .Proxy.MixIns(testProxy));

测试类如下:

public class TestClass : ITestClass
{
    public TestClass()
    {

    }        

    public string Name
    {
        get;
        set;
    }
    public void Checkin()
    {
        Name = "Checked In";
    }
}

拦截器很简单,如果名称以Check开头,只需输入一个方法即可。

现在,当我从容器中解析我的 TestClass 时,我收到错误。

{“这是一个 DynamicProxy2 错误:Mixin 类型 TestClassProxy 实现了 IProxyTargetAccessor,它是一个 DynamicProxy 基础结构接口,您永远不应该自己实现它。您是否尝试混合现有代理?”}

我知道我使用了错误的代理方式,但由于我还没有看到任何关于如何将代理与温莎容器一起使用的具体示例,这有点令人困惑。

我的意思是,如果我想使用 LoggingProxyGenerationHook ,它只是告诉拦截器首先使用以“check”一词开头的方法,那么这是正确的方法还是我完全走错了路。我刚刚采用了代理方式,因为它看起来非常强大,我想了解如何使用这些代理来进行未来的编程工作。

I've got a bit of a problem. I'm working in the Castle Windsor IOC Container. Now what i wanted to do is just mess about with some AOP principles and what i specifically want to do is based on a method name perform some logging. I have been looking at Interceptors and at the moment i am using the IInterceptor interface implemented as a class to perform this logging using aspects. The issue is if i want to perform the logging on a specific method then it gets messy as i need to put in some logic into my implemented aspect to check the method name etc...

I have read that you can do all of this using Dynamic Proxies and the IInterceptorSelector interface and the IProxyGenerationHook interface. I have seen a few examples of this done on the net but i am quite confused how this all fits into the Windsor container. I mean i am using the windsor container which in my code is actually a ref to the IWindsorContainer interface to create all my objects. All my configuration is done in code rather than XML.

Firstly does anyone know of a way to perform method specific AOP in the windsor container besides the way i am currently doing it.

Secondly how do i use the Dynamic Proxy in the windsor container ?

Below i have added the code where i am creating my proxy and registering my class with
the interceptors

ProxyGenerator _generator = new ProxyGenerator(new PersistentProxyBuilder());
IInterceptorSelector _selector = new CheckLoggingSelector();
var loggingAspect = new LoggingAspect();
var options = new ProxyGenerationOptions(new LoggingProxyGenerationHook()) 
                  { Selector = _selector };
var proxy = _generator.CreateClassProxy(typeof(TestClass), options, loggingAspect);
TestClass testProxy = proxy as TestClass;

windsorContainer.Register(
Component.For<LoggingAspect>(),
Component.For<CheckLoggingAspect>(),
Component.For<ExceptionCatchAspect>(),
Component.For<ITestClass>()
    .ImplementedBy<TestClass>()
    .Named("ATestClass")
    .Parameters(Parameter.ForKey("Name").Eq("Testing"))
    .Proxy.MixIns(testProxy));

The Test Class is below:

public class TestClass : ITestClass
{
    public TestClass()
    {

    }        

    public string Name
    {
        get;
        set;
    }
    public void Checkin()
    {
        Name = "Checked In";
    }
}

as for the interceptors they are very simple and just enter a method if the name starts with Check.

Now when i resolve my TestClass from the container i get an error.

{"This is a DynamicProxy2 error: Mixin type TestClassProxy implements IProxyTargetAccessor which is a DynamicProxy infrastructure interface and you should never implement it yourself. Are you trying to mix in an existing proxy?"}

I know i'm using the proxy in the wrong way but as i haven't seen any concrete example in how to use a proxy with the windsor container it's kind of confusing.

I mean if i want to use the LoggingProxyGenerationHook which just tell the interceptors to first for methods that start with the word "check" then is this the correct way to do it or am i completely on the wrong path. I just went down the proxy way as it seems very powerfull and i would like to understand how to use these proxies for future programming efforts.

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

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

发布评论

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

评论(1

执手闯天涯 2024-08-30 07:07:48

通过使用.Interceptors(),您已经在使用动态代理。当组件指定了拦截器时,Windsor 将为它创建代理,并为其使用这些拦截器。您还可以使用方法 .SelectedWith.Proxy 属性来设置您已从 DynamicProxy 中了解的其他选项。

我刚刚向文档 wiki 添加了一个有关 Windsor AOP 的网站。目前还没有太多信息,但我(和毛里西奥;))会将您需要的所有信息放在那里。看一下,如果一切都清楚,以及是否缺少某些内容,请告诉我们。

By using .Interceptors() you already are using Dynamic Proxy. When component has specified interceptors Windsor will create proxy for it, and use these interceptors for it. You can also use method .SelectedWith and .Proxy property to set other options you already know from DynamicProxy.

I just added a website about Windsor AOP to documentation wiki. There's not much there yet, but I (and Mauricio ;) ) will put there all the information you need. Take a look, and let us know if everything is clear, and if something is missing.

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