如何运行代理POCO?(How to runtime proxy POCOs?)

编程入门 行业动态 更新时间:2024-10-23 20:15:47
如何运行代理POCO?(How to runtime proxy POCOs?)

我希望能够使用JRE或cglib代理库以与Java相同的样式为POCO创建运行时代理。

我见过Castle DynamicProxy看起来很符合要求,但文档并不好。 我也很惊讶这种事情不在.Net框架本身。

有没有人:

知道Castle DynamicProxy有什么好的资源吗? 知道任何替代方法吗?

谢谢!

I would like to be able to create a runtime proxy for a POCO, in the same style as I might in Java using the JRE or cglib proxying libraries.

I've seen Castle DynamicProxy which looks like it fits the bill, but the documentation isn't great. I'm also surprised that this kind of thing isn't in the .Net Framework itself.

Does anyone:

Know of any good resources for Castle DynamicProxy? Know of any alternative approaches?

Thanks!

最满意答案

这可能不值得回答,但是你没有看到.NET中动态代理的激增的一个原因是,最常见的.NET语言(C#,VB.NET)与Java的不同之处在于一个非常重要的方面:

在Java中,除非明确声明密封,否则所有方法都是虚拟的

在C#(和VB.NET IIRC)中,除非显式声明为virtual,否则所有方法和属性都是密封的。

这意味着.NET中动态代理的潜在价值远低于Java。 您必须明确地将.NET对象设计为“可代理”,并且大多数人不这样做 - 需要有意识地决定使.NET对象“可代理”。

实际上很少见到DI容器之外的.NET动态代理,我想不出除Castle之外的任何其他动态代理。

请注意,“默认密封”行为是.NET 语言的一个特性 - 可以想象,人们可以设计一种基于.NET的语言,该语言具有与Java相同的“默认虚拟”行为。 虽然我不知道,但如果这种语言不存在,我会感到惊讶。 毕竟,它都在编译器中。

This may not be worth much as answers go, but one of the reasons that you don't see a proliferation of dynamic proxies in .NET is that most common .NET languages (C#, VB.NET) differ from Java in one very important aspect:

In Java, all methods are virtual unless explicitly declared sealed.

In C# (and VB.NET IIRC) all methods and properties are sealed unless explicitly declared virtual.

This means that the potential value of a dynamic proxy is far lower in .NET than it is in Java. You have to explicitly design your .NET objects to be 'proxyable', and most people don't do that - it takes a conscious decision to make a .NET object 'proxyable'.

It's actually so rare to see a .NET dynamic proxy outside of DI Containers that I can't think of any other dynamic proxies than Castle.

Note that the 'sealed by default' behavior is a feature of the .NET languages - it is very conceivable that one could design a .NET-based language that has the same 'virtual by default' behavior as Java. Although I don't know any, I would be surprised if such a language does not exist. After all, it's all in the compiler.

更多推荐

本文发布于:2023-07-16 07:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:runtime   POCO   POCOs   proxy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!