【发布时间】:2014-06-18 04:10:13
【问题描述】:
我继承了一个用 WinForms 编写的大型项目,我必须进行很多小改动。我想使用 AOP(Castle.DynamicProxy 和 Autofac),到目前为止还没有给我带来问题。我写了一个简单的Aspect:
class TestAspect : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Satrt method" + invocation.Method.Name);
invocation.Proceed();
Console.WriteLine("Stop funkcji " + invocation.Method.Name);
}
}
接下来,我在 ContainerBuilder 中注册类型。例如:
builder.RegisterType<SomeClass>().As<ISome>().EnableInterfaceInterceptors().InterceptedBy(typeof(TestAspect));
builder.RegisterType<TestAspect>();
到目前为止,一切都很好。但是我必须将这方面添加到遗留代码中(写得不好)。我必须将方面代码(不仅是上面的代码)显式添加到 WinForms。例如,我有启动方法 CalculateSomething 的按钮(在当前窗口中,不在类中)。在 ContainerBuilder 中,我注册了当前表单:
builder.RegisterType<Form1>().EnableClassInterceptors().InterceptedBy(typeof(TestAspect));
但 EnableClassInterceptors() 仅适用于虚拟方法。 我知道旧代码需要重构,但客户已经使用了这个程序。 我们重写了这段代码,但我们需要快速进行一些更改。 我有两个解决方案:
- 将所有方法设为虚拟(非常糟糕的解决方案)
- 手动将此 Aspect 代码添加到每个方法(更糟糕的解决方案)
当然,只要我们重构了遗留代码,我们就会做对。但是现在怎么办? 也许有人有任何想法?
【问题讨论】:
标签: c# winforms aop autofac castle-dynamicproxy