码迷,mamicode.com
首页 > 其他好文 > 详细

Castle DynamicProxy

时间:2019-05-20 17:01:01      阅读:558      评论:0      收藏:0      [点我收藏+]

标签:-o   stl   without   alt   option   object   res   sse   memory   

https://github.com/castleproject/Core/blob/master/docs/dynamicproxy.md

DynamicProxy generates proxies for your objects that you can use to transparently add or alter behavior to them, provide pre/post processing and many other things. Following are just a few better known and popular usages of DynamicProxy:

  • Castle Windsor uses proxies to enable its interception capabilities and for typed factories
  • Moq uses it to provide "the most popular and friendly mocking framework for .NET"
  • NSubstitute uses it to provide "a friendly substitute for .NET mocking frameworks"
  • FakeItEasy uses it to provide "the easy mocking library for .NET"
  • Rhino Mocks uses it to provide "a dynamic mock object framework for the .NET platform"
  • NHibernate uses it to provide lazy loading capabilities (pre-v4.0)
  • Entity Framework Core uses it in its package Microsoft.EntityFrameworkCore.Proxies to provide lazy-loading proxies

If you‘re new to DynamicProxy you can read a quick introduction, browse through description of the core types in the library, or go into more advanced, in detail discussion:

??Where is Castle.DynamicProxy.dll?: DynamicProxy used to live in its own assembly. As part of changes in version 2.5 it was merged into Castle.Core.dll and that‘s where you‘ll find it.

??Use of a single ProxyGenerator‘s instance: If you have a long running process (web site, windows service, etc.) and you have to create many dynamic proxies, you should make sure to reuse the same ProxyGenerator instance. If not, be aware that you will then bypass the caching mechanism. Side effects are high CPU usage and constant increase in memory consumption.

 

Kinds of Proxy Objects

Out of the box, the DynamicProxy provides several kinds of proxy objects that you can use. They fall into two broad categories:

 

Inheritance-based

Inheritance-based proxies are created by inheriting a proxy class. The proxy intercepts calls to virtual members of the class and forwards them to base implementation. In this case, the proxy and proxied object are one. This also means you can‘t create inheritance based proxy for a pre-existing object. There‘s one inheritance-based proxy kind in DynamicProxy.

  • Class proxy - creates an inheritance-based proxy for a class. Only virtual members of the class can be intercepted.

 

Composition-based

Composition-based proxy is a new object that inherits from proxied class/implements proxied interface and (optionally) forwards intercepted calls to the target object. DynamicProxy exposes the following composition-based proxy types:

  • Class proxy with target - this proxy kind targets classes. It is not a perfect proxy and if the class has non-virtual methods or public fields these can‘t be intercepted giving users of the proxy inconsistent view of the state of the object. Because of that, it should be used with caution.

  • Interface proxy without target - this proxy kind targets interfaces. It does not expect target object to be supplied. Instead, interceptors are expected to provide implementation for all members of the proxy.

  • Interface proxy with target - as its name implies wraps objects implementing selected interfaces forwarding calls to these interfaces to the target object.

  • Interface proxy with target interface - this kind of proxy is kind of a hybrid of two other interface proxy kinds. It allows, but not requires target object to be supplied. It also allows the target to be swapped during the lifetime of the proxy. It is not tied to one type of the proxy target so one proxy type can be reused for different target types as long as they implement the target interface.

 

External resources

 

Castle DynamicProxy

标签:-o   stl   without   alt   option   object   res   sse   memory   

原文地址:https://www.cnblogs.com/chucklu/p/10894797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!