Browse DevX
Sign up for e-mail newsletters from DevX


Writing Advanced .NET Remoting Clients : Page 3

.NET Remoting provides a powerful way to use remote objects as if they existed locally, using interfaces and the factory pattern.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

After defining the interface you can create an implementation. It's best to create the implementation in a separate class library project to make it easy to use the classes in other projects.

public class CorporateAccountantTest : MarshalByRefObject, IAccountantTest { private string name; public CorporateAccountantTest() { name="David"; } public string WhatIsTwoPlusTwo(int answer) { if(answer==4) { return "That's an "+ "acceptable answer, " + name + "."; } else { return "That's right " + name + ", 2+2 is whatever you want it to be!"; } public void SetName(string name) { this.name = name; } }

The CorporateAccountantTest class inherits from MarshalByRefObject and also implements the IAccountantTest interface. Your Remoting classes must be subclasses of MarshalByRefObject, otherwise you will get a mysterious "not serializable" error.

public class CorporateAccountantExamFactory : MarshalByRefObject, IAccountantExamFactory { public IAccountantTest CreateAccountTestInstance() { return new CorporateAccountantTest();} } }

The only purpose of the CorporateAccountantExamFactory class is to create a CorporateAccountantTest instance and return it to the client as an interface that the client can treat only as IAccountantTest. You use the factory pattern because you want the client side object to be able to create instances of the object on the server using only its knowledge of the interfaces. You can't create an instance of an interface using the new keyword so the factory helps out by letting the client create new instances of the object using a simple method call.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date