什么是控制反转/面向方面意义上的 .NET 代理对象?

发布于 2024-07-09 08:04:31 字数 97 浏览 12 评论 0原文

什么是控制反转/面向方面意义上的代理对象?

有什么关于什么是代理对象的好文章吗?
为什么你想使用一个?
以及如何用 C# 编写一个?

What is a proxy object in the Inversion of Control / Aspect-Oriented sense?

Any good articles on what a proxy object is ?
Why you would want to use one ?
And how to write one in C# ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他不在意 2024-07-16 08:04:31

一般来说,代理对象是一个对象(类的实例),它公开与“真实类”完全相同的公共接口,但只是将对其成员的所有调用转发到另一个真实类。 使用代理对象的原因有多种...

一个目的是“假装”为真实的类,以便客户端组件(或对象)可以“相信”它正在与“真实”对象通信,但在代理内部,其他东西(如日志记录、事务支持等)同时进行......其次,与真实对象相比,代理可能非常便宜。 并且经常被使用,以便当客户端不使用它们时,真实的对象可以被保存(关闭或释放到池中以供其他客户端使用)...代理保持“活动”并且客户端认为它仍然有与真实对象的连接,但每当它“调用”该对象时,它实际上是在调用代理,代理会获取另一个真实对象来处理调用,然后在调用完成时释放该真实对象。

至于控制反转(IOC)......这是指一种常见模式(也称为依赖注入),其中类内部的依赖对象被“注入”到该类的实例中,从客户端代码中,控制实例将使用哪个版本的依赖对象... IOC 可用于将“代理”对象注入到它认为正在使用真实对象的类中...短语 控制反转指的是这样一个事实:当使用此模式时,调用哪个实际实现的决定不再由进行调用的类控制,而是由该类的客户端控制,当它 <将依赖对象的实例注入到用于此调用的类中。

通常,术语 IOC 与所谓的“IOC 容器”一起使用,这是一个专门设计的类,负责根据有关其获取的类(类型)的松散耦合信息创建依赖类的实例来自硬连线依赖项之外的其他来源(最常见的是来自某种配置文件)。 通常,当您使用 IOC 容器时,您会在应用程序启动时创建它的实例,然后(通过读取配置数据或其他方式)“注册”IOC 容器将负责的每个类(类型) ,具有关键值。 关键通常是该注册的所有实例都必须实现的抽象类型或接口)。 然后,在应用程序的正常操作中,您可能会新建其中一种类型的实例,您可以调用 IOC 容器,并使用抽象类型/接口作为键来请求它提供一个实例。 然后,IOC 容器使用反射或动态加载(或其他方式)来创建已使用该键“注册”的任何类型的实例。 这样,只需更改配置数据,您就可以控制应用程序使用的实际类型,将一个环境或部署位置中使用的类型与另一环境或部署位置中使用的类型进行更改。

In general, a Proxy object is an object (instance of a class) that exposes the exact same public interface as a "real class" but simply forwards all calls made to it's members to the other real class. Proxy objects are used for a variety of reasons...

One purpose is to "pretend" to be the real class so a client component (or object) can "believe" it's talking to the "real" object, but inside the proxy, other stuff, (like logging, transactional support, etc.) is being done at the same time... Secondly, a proxy can be very cheap in comparson to the real object,. and often is used so that the real objects can be conserved (turned off or released to a pool to be used by other clients) when the client is not using them... The proxy stays "alive" and the client thinks it still has a connection to the real object, but whenever it "calls" the object, it is actually calling the proxy, which goes and gets another real object just to handle the call, and then releases the real object when the call is done.

As to Inversion of Control (IOC).. That refers to a common pattern (also referred to as Dependency Injection), where dependant objects inside of a class are "injected" into an instance of the class, from client code, to control which version of a dependant object the instance will use... IOC can be used to inject a "Proxy" object into a class where it thinks it is using the real object... The phrase Inversion of Control refers to the fact that when using this pattern, the decision as to which actual implementation is called is no longer under the control of the class making the call, but to the client of that class, when it injects an instance of a dependant object into the class to be used for this call.

Generally the term IOC is used with what is called an IOC Container, which is a class specifically designed to be responsible for creating instances of dependant classes based on loosely coupled information about those classes (Types) which it gets from some source other than hard-wired dependencies (most often, from some kind of configuration file). Generally, when you use an IOC container, you create an instance of it when the application starts, and then (by reading config data or whatever), you "register" each of the classes (types) that the IOC container will be responsible for, with a key value. The key is often the abstract type or interface that all instances of this registration must implement). Then, in the normal operations of your application, where you might otherwise have new'd up an instance of one of these types, you call the IOC Container, and ask it for an instance instead, using the abstract type/Interface as the key. The IOC container then uses reflection or dynamic loading, (or whatever), to create an instance of whatever type has been "registered" with that key. In this way, simply by changing configuration data, you can control the actual types used by the application, changing them in one environment or deployment location from those used in another.

神经暖 2024-07-16 08:04:31

关于这方面的一个非常好的资源是旧的“Gang of Four”设计模式书。 本书对于任何开发面向对象软件的人都非常有用。
我个人使用代理对象来通过 NHibernate 进行延迟加载。 我不使用具有控制反转的代理,因为我仅使用 IoC 解析接口类型。

Charles Bretana 的解释非常好。

我无法想象proxy和AoP之间的关系。 有人可以在这里解释一下吗?

A very good resource about this is the old "Gang of Four" design patterns book. This book is very usefull for anyone developing object-oriented software.
I'm personally using proxy objects for lazy loading with NHibernate. I don't use proxies with inversion of control because I resolve interfaced types only with my IoC.

Charles Bretana's explanation is very good.

I can't imagine the relation between proxy and AoP. Could someone explain that here?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文