I thought that I could not use Delphi to implement dynamic proxy last weekend, but I have never been able to put down this idea for the past few days. This is really a wonderful idea. After carefully reading the implementation of soap by VCL, we can achieve such a dynamic proxy with at least nine points of confidence.
So what is the use of dynamic proxy?
This should begin with the gof proxy mode.
Assume that the following interface and its implementation are available:
Now, if you are the user of this interface, and this interface and its implementers provide one:
Foo : IFoo;
Here, foo points to an instance of tfooimpl. Now you have the ifoo definition, and the foo instance -- note that you do not have the tfooimpl definition and implementation code. What if you want to add the transaction function for all ifoo. dosomething (assuming that dosomething is implemented as an update operation on the database?
The gof proxy mode is one of the solutions:
As shown in, you must first implement a new ifoo interface-tstaticproxy. A fimpl is used to record the tfooimpl instance. In tstaticproxy, implement dosomething and bar, and delegate bar functions that do not need to be changed directly to fimpl for processing, and add transaction processing to the implementation of dosomething. The tstaticproxy code is roughly as follows:
TStaticProxy = class( TInterfacedObject, IIFoo )private FImpl : IFoo;public constructor Create( aImpl : IFoo ); function doSth( ... ) : xxx; function bar( ... ) : xxx;end;constructor TStaticProxy.Create( aImpl : IFoo );Begin FImpl := aImpl;End;function TStaticProxy.doSth( ... ) : xxx;Begin BeginTransaction; Result := FImpl.doSth( ... ); EndTransaction;End;function TStaticProxy.bar( ... ) : xxx;Begin Result := FImpl.bar( ... );End;
Then, switch to the new newfoo object where the foo object is needed, as shown below:
VaR newfoo: ifoo; begin newfoo: = tstaticproxy. Create (FOO) as ifoo;... // then newfoo can be used as Foo. End;
It can be seen that we use a proxy class to proxy all the operations on the ifoo interface, which is equivalent to inserting our own code between ifoo and tfooimpl. To some extent, this is the so-called "cross-cutting" of AOP ". Of course, if you can have tfooimpl code, it will be simple, as long as the following:
Derive a tnewfooimpl from tfooimpl, override the dosomething in tfooimpl, and then create a tnewfooimpl instance to replace the foo reference.
But the problem is that you have to use tfooimpl code and can change the provided Foo instance, but this is often not possible-unless you do not use Delphi, it is a dynamic language like python ^ o ^. Such as component containers, such as remote instances. There is also a "virtual proxy" (that is, when the cost of creating a fimpl is very high, only the proxy class is created at the time of creation, and then the fimpl instance is created only when it is really needed)
But the above static proxy is still very troublesome. First, if there are many ifoo member functions, you must add proxy implementation to them one by one. Second, if there are many interfaces in the application that require proxy, you must write such a dedicated proxy class for them one by one. Third, you need to modify all the proxy classes when you need to change the proxy function ......
Especially for component containers or general remote proxies, static proxies are not used at all when the interface to be implemented is uncertain.
Therefore, we need "dynamic proxy ". After reading gigix's article titled "the past and present of dynamic proxy" published in the first issue of "programmers" this year, although he said it was proposed by Java in jdk1.3. lang. proxy in reflect. However, I was surprised to find that such a dynamic proxy can be fully implemented in Delphi.
A typical dynamic proxy is as follows:
In this way, we only need to add tfooinvhandler in the function to be an instance of the iinvocationhandler interface. Then dynamically create a tdynamicproxy instance that supports the ifoo interface-it is a dynamic proxy, you only need to input the corresponding parameters: the interface to be implemented and the corresponding invhandler instance, you do not need to write a proxy for each interface. Of course, as gigix said, for C ++, this can be implemented using templates, but the problem is that templates are a dynamic technology during compilation, for an application that requires dynamic runtime such as component containers, it still cannot be implemented. Finally, invhandler calls the specific implementation Foo through rtti.
Its usage is roughly as follows:
Tfooinvhandler = Class (tinterfacedobject, iinvocationhandler) Private fimpl: ifoo; Public constructor create (aimpl: ifoo); function invoke (IID, methodname, argS []): XXX; end; constructor tfooinvhandler. create (aimpl: ifoo); begin fimpl: = aimpl; end; function tfooinvhandler. invoke (IID, methodname, argS []): xxxbegin if (IID = ifoo) and (methodname = 'docs') then begin begintransaction; R Esult: = doinvoke (fimpl, IID, methodname, argS []); endtransaction; end else result: = doinvoke (fimpl, IID, methodname, argS []); end; vaR handler: iinvocationhandler; newfoo: ifoo; begin handler: = tfooinvhandler. create (FOO); newfoo: = tdynamicproxy. create (typeinfo (ifoo), Handler) as ifoo ;... // then newfoo can be used as Foo. End;
Note: I haven't thought about how to define the iinvocationhandler interface, so this code just outlines the problem. In addition, doinvoke calls fimpl through rtti.
From the code above, tdynamicproxy dynamically generates a proxy for the ifoo interface through the ifoo parameter, and inserts a processing interface iinvocationhandler through the handler parameter, tdynamicproxy converts all the calls to the ifoo interface to the call to the iinvocationhandler interface. Finally, the tfooinvhandler class handles the call as needed. Here, you can dynamically decide whether to switch to the transaction service through the runtime configuration mode, and which method of the interface to be switched.
With such a dynamic proxy, you can easily cut in the invocationhandler, such as security check and log. In this case, using Delphi to implement AOP is not a problem.
Now I am faced with the problem of how to define this iinvocationhandler.
In fact, the main problem here is the parameter transfer problem. The interface can be represented by IID, and the method name can be used, but the parameter changes too much. One is that the number is uncertain and there can be any number of parameters. The other is that the type is uncertain; third, the problem of passing value parameters and reference parameters. For example, in the previous example, a simple method is to use an indefinite variant array to record and solve the first two problems, but the third problem is troublesome, is it necessary to use a tuple as the return value? It's too troublesome.
In the SOAP implementation of VCL, A tinvcontext is recorded. However, for Handler developers, they have to face the internal complexity and ease of use of tinvcontext.
This is the achievement that I cannot determine yet. -_-|
Feb.03-05