Tuesday, October 28, 2008

Remoting through interfaces

When we are going to use Remoting, we always think about the proxy classes, which should surely be deployed with the client-app. But is that the best approach?
The OOP tells us to understand the interaction through the intercaces, this means that components should interact only by contracts. Why don’t we use this approach in Remoting. This is surely possible. And this is better approach.
Let’s review this part of code:

public class RemotedObject:MarshalByRefObject
{
public void SomeMethod()
{
//… some code here
}
}

Let’s update the code above and add a contract of interacting with it:

public class RemotedObject:MarshalByRefObject, IRemotedObjectContract
{
public void SomeMethod()
{
//… some code here
}
}

//The interface is defined in a new module
public interface IRemotedObjectContract
{
void SomeMetod();
}

The remoted object now can be called through IRemotedObjectContract interface. But we should place the interface definition in a separate module, so it will be referenced from the main (RemotedObject) assembly, and also it will be referenced from the client. So now, the client have not to generate a proxy class by hand and attach that module, but it will be enough just to reference the interface-owner module and all is ready. Notice, that nothing should change on the server side. But on the client side for accessing the object we will have something like:


//…
IRemotedObjectInterface tmpRemote = (IRemotedObjectInterface)Activator.GetObject(typeof(IRemotedObjectInterface), tmpUrl);
//Use the retrieved tmpRemote interface to interact with the remote object


So, this is all, and this is really good approach in .Net Remoting.

No comments: