AOP、Unity、方法拦截
我刚刚开始使用 Unity 和拦截功能。我创建了一个原型来验证限制或功能,在我看来,为了拦截方法,必须满足几个条件。特别是在我看来,类或接口必须通过容器来解析。这是正确的吗?有没有什么方法可以让我在没有这个要求的情况下做到这一点。
我可能会误解如何正确使用它,因为有很多旧的和部分的信息没有好的工作示例。
我将更新一些代码。请记住,这个东西有成千上万行代码,因此改变这个糟糕的设计不是一个选择,并且超出了当前项目的范围。
public interface IApoClass
{
[LoggingCallHandler]
void SomeAbstractAopMethod(string abstractparam);
[LoggingCallHandlerAttribute]
void SomeVirtualAopMethod(string virtualparam);
}
public abstract class AbstractAopClass : IApoClass
{
public abstract void SomeAbstractAopMethod(string whatthe);
public virtual void SomeVirtualAopMethod(string abstractparam)
{
//essentiall do nothing
return;
}
}
public class NonAbstractAopMethod : AbstractAopClass
{
public override void SomeAbstractAopMethod(string whatthe)
{
Console.Write("I am the only enforced method");
}
}
容器:
container.AddNewExtension<Interception>();
//container.Configure<Interception>().SetInterceptorFor<IFrameworkInterceptionTest>(new InterfaceInterceptor());
container.Configure<Interception>().SetInterceptorFor<IApoClass>(new InterfaceInterceptor());
调用代码:
//resolve it despite it not having and definition, but we've wired the container for the interface implemented by the abstract parent class
var nonabstractmethod = DependencyResolver.Current.GetService<NonAbstractAopMethod>();
nonabstractmethod.SomeAbstractAopMethod("doubt this works");
nonabstractmethod.SomeVirtualAopMethod("if the above didn't then why try");
I have just started using Unity and the interception capabilities. I created a prototype to verify the limitation or capabilities and it seems to me that in order to intercept a method there are several condition that must be met. In particular it seems to me that the class or interface must be resolved through the container. Is that correct? Is there any method out there that let's me do this without this requirement.
I may be misinterpreting how to use this properly as there is alot of older and partial information out there without good working examples.
I will update with some code. Please keep in mind there are thousands upon thousands of lines of code in this thing so changing this bad design is not an option and out of scope of this current project.
public interface IApoClass
{
[LoggingCallHandler]
void SomeAbstractAopMethod(string abstractparam);
[LoggingCallHandlerAttribute]
void SomeVirtualAopMethod(string virtualparam);
}
public abstract class AbstractAopClass : IApoClass
{
public abstract void SomeAbstractAopMethod(string whatthe);
public virtual void SomeVirtualAopMethod(string abstractparam)
{
//essentiall do nothing
return;
}
}
public class NonAbstractAopMethod : AbstractAopClass
{
public override void SomeAbstractAopMethod(string whatthe)
{
Console.Write("I am the only enforced method");
}
}
The container:
container.AddNewExtension<Interception>();
//container.Configure<Interception>().SetInterceptorFor<IFrameworkInterceptionTest>(new InterfaceInterceptor());
container.Configure<Interception>().SetInterceptorFor<IApoClass>(new InterfaceInterceptor());
The calling code:
//resolve it despite it not having and definition, but we've wired the container for the interface implemented by the abstract parent class
var nonabstractmethod = DependencyResolver.Current.GetService<NonAbstractAopMethod>();
nonabstractmethod.SomeAbstractAopMethod("doubt this works");
nonabstractmethod.SomeVirtualAopMethod("if the above didn't then why try");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的假设是正确的:如果您希望使用拦截,您需要通过容器解析对象,以便 Unity 可以创建代理对象或返回派生类型。
Unity 拦截技术 做得很好解释它是如何工作的。
如果我理解正确的话,您希望对接口方法进行拦截,同时尽量减少对现有系统的更改。
您应该能够连接拦截。首先注册接口和要映射到的具体类型,然后设置拦截:
如果需要将接口映射到多个具体类型,可以通过使用名称来实现:
Your assumption is correct: if you wish to use interception you need to resolve the object through the container so Unity can create a proxy object or return a derived type.
Unity Interception Techniques does a good job of explaining how it works.
If I understand you correctly, you want to perform interception for interface methods while minimizing changes to the existing system.
You should be able to wire up the interception. First register the interface and the concrete type you want to map to and then set the interception:
If you need to map the interface to multiple concrete types you can do that by using a name: