What is a .NET proxy object in a control / aspect-oriented sense inversion?

What is a proxy object in control inversion / aspect-oriented meaning?

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

+4
source share
2 answers

In general, a proxy object is an object (an instance of a class) that provides the same public interface as the "real class", but simply forwards all the calls made to it by members of another real class. Proxy objects are used for various reasons ...

One goal is to “pretend” to be a real class, so the client component (or object) can “believe” that it is talking to the “real” object, but inside the proxy server, etc. (for example, logging, transactional support, etc.) is performed simultaneously ... Secondly, a proxy can be very cheap compared to a real object. and it is often used so that real objects can be saved (disabled or released into a pool that will be used by other clients) when the client does not use them ... The proxy server remains "alive" and the client considers that it is still has a connection with the real object, but whenever it “calls” the object, it actually calls the proxy server, which goes and receives another real object only to handle the call, and then releases the real object when the call is made.

As for Inversion of Control (IOC) .. This refers to the general template (also called Injection Dependency), where the dependent objects inside the class are “injected” into the class instance, from the client code, to control the version of the dependent object that the instance will use .. IOC can be used to enter the "Proxy" object into a class where it believes that it is using a real object ... The phrase Inversion of control refers to the fact that when using this template, the decision about which actual implementation is called is no longer controlled class Causing the call, and this class of customer, when he enters the instance of a dependent object in the class that will be used for this call.

Usually, the term IOC is used with the so-called IOC container, which is a class specially designed to create instances of dependent classes based on loosely coupled information about these classes (types) that it receives from some other source than hard links (most often, from any configuration file). As a rule, when you use the IOC container, you create an instance of it when the application starts, and then (reading the configuration data or something else) you "register" each of the classes (types) that the IOC container will respond to with the key value . The key is often an abstract type or interface that all instances of this registration should implement). Then in the normal operations of your application, where you could otherwise create an instance of one of these types, you call the IOC container and request it instead, using the abstract type / interface as the key, then the IOC container uses reflection or dynamic loading (or whatever else) to create an instance of any type that has been "registered" with this key. Thus, 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.

+15
source

A very good resource about this is the old gang of four models book. This book is very useful for those who develop object-oriented software. I personally use proxy objects for lazy loading using NHibernate. I do not use proxies with inverse control because I only allow conjugate types with my IoC.

Charles Brittany has a very good explanation.

I can not imagine the connection between proxies and AoP. Can anyone explain this here?

+1
source

All Articles