.net 动态代理的成本
使用动态代理的成本是多少?
我不想用接口实现使我的项目变得混乱,所以我正在考虑使用由一些第三方库(如 LinFu 、 Castle 、 Unity 等)创建的动态代理。它们是为每个接口生成一个实例还是我为每个调用生成一个实例。
它是一个网络应用程序,那么从长远来看,性能问题是什么?
我也在使用 EF 4.1(目前是 CTP5),所以如果本身创建代理类,这让我想知道是否可以使用 EF 自己的动态代理创建工具。
PS 是的,我的接口是由具体类以及其他接口和基类实现的,但有时我只需要它的接口部分,而不需要具体类附带的额外内容。
所有接口仅声明 EF4.1 POCO 的一部分。所以只是 getter 和 setter。
What is the cost of using dynamic proxies?
I do not want to clutter my project with Interface Implementations, so I am considering using Dynamic proxies created by some 3rd party library like LinFu , Castle, Unity etc. Do they generate one instance per interface or do I get one for each call.
It is a web app, so whats the performance problem in the long run.
I am also using EF 4.1 (CTP5 at the moment), so if does create proxy classes itself, which makes me wonder if I can use EF's own Dynamic Proxy creating tools.
P.S. yes my interfaces are implemented by concrete classes along with other interfaces and base classes, but sometimes I only need the interface portion of it and not the extra stuff that comes with the concrete class.
All interfaces declare just some part of an EF4.1 POCO. So just getters and setters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
开源Impromptu-Interface,需要 c# 4.0,并为每个接口创建一个轻量级代理类型您使用的接口和实现类型组合并将它们缓存起来。
因此,围绕给定的实现创建接口代理(无论您如何设置,ExpandoObject 都算作一种类型)将产生生成代理类型的一次性成本,即每次创建代理时都会产生一个 Activator.CreateInstance(这不是不错)并且对于每个调用都会有一个静态调用,这是您在没有代理的情况下所拥有的 + dlr 动态调用,这要归功于微软,这是非常优化的。
The opensource Impromptu-Interface, requires c# 4.0, and creates a lightweight proxy type for each Interface and Implementation Type combo you use and keeps them cached.
So creating an interface proxy around a given implementation (an ExpandoObject counts as one type no matter how you set it up) will have a one time cost of generating the proxy type, an Activator.CreateInstance for each time you make a proxy (which isn't bad) and for each call there will be a static invocation which is what you'd have with out the proxy + a dlr dynamic invocation which is very optimized thanks to microsoft.
看起来您需要更多的存根而不是动态代理。也许您可能想看看Moq。据我所知,它会在每次创建模拟时创建一个不同的实例,但不知道内部是否保留某种类型缓存。请注意,因为它是一个针对单元测试的库,所以这种使用可能是非正统的。
Looks like you need more of a stub rather than a dynamic proxy. Perhaps you might want to take a look at Moq. As far as I know it creates a different instance for every time you create a mock, don't know if internally keeps some sort of a Type cache, though. Mind you, as it's a library targeted for unit tests, so this kind of use would be probably unorthodox.