When you look at the RTL Changes for XE2 you will see a new addition to the RTTI - TVirtualInterface. Along with this comes another improvement: RTTI can finally tell the implemented interfaces of a class (and its ancestors). But back to the TVirtualInterface!
Documentation says: "Provides functionality for remote procedure call marshaling. TVirtualInterface creates an implementation of an interface at run time." Wow, this sounds interesting. Further reading states that it's main purpose is SOAP messaging. A client can consume a service with several methods just by calling them through an interface which then "just calls" the remote methods of the service. I think this is awesome because it leads to clear defined interfaces (literally) when working with remote services or other things that just hide behind the interface and which are not necessary to be known. Other things I say...
Some while ago when I came across the Emballo Framework I was impressed by the DLLWrapper which basically imports exported functions of a DLL and provides them through an interface. Of course there have to be some rules like the calling conventions and the parameters have to match. But you can do define an interface like this:
type IMyLibrary = interface ['{8B47F556-673B-44D6-8F90-09985B3C53E0}'] function SayHello: string end;
And then simply import a DLL which exports this function and just call it. It was a bit rough and actually only supported a few types and not all calling conventions. So I am very happy to have a tool out of the box that makes it possible to write something like this:
if Supports('MyLibrary.dll', IMyLibrary, LMyLibraryIntf) then ShowMessage(LMyLibraryIntf.SayHello());
Those of you that worked with .Net in RAD Studio 2007 may know this - it was called Virtual Library Interfaces. I have not tried yet, but I am sure if you have the correct signature in your interfaces methods you can call almost every DLL. Actually the Supports functions does not exist in Delphi but you can find it in the brand new unit DSharp.Core.Plugins.
I uploaded a simple sample how to use this (including the new unit which can also be downloaded from the svn repository).
I also added the DSharp packages for XE2 today (only 32-bit for now, 64-bit compatibility will follow soon). I know there is some issue with the asm code in the DSharp.Core.Events (that has to go for 64-bit anyway) which break bindings for now - but I am working on it. Also more about LiveBindings vs DSharp soon.
Hey! Shouldn't this also allow "automatic mocks"? I.e. no more having to create explicit mock classes for each and every mocked interface?
ReplyDeleteFrom looking at the example, it looks like it does...
Great News! :)
Hi. I have the same impression as the Oliver.
ReplyDeleteSeems possible get the same result with the implementation of the interface using anonymous methods. If so, we really can create dynamic mock objects.
Great