Click to See Complete Forum and Search --> : Problems with abstract factory and casting


torrud
March 9th, 2005, 10:31 AM
Hi,

I wrote a windows-service for handling files. Therefore I use Remoting. Because I am lazy. I wrote an abstract factory for creating broker-objects.
Here is the body:
public object GetBroker(Type brokerType)
{
//implementation
}
This method works great and instancing the correct classes. But I have a problem to cast the object against a given Interface. Because I use remoting, I have a shared dll which contains the interface of the broker. And the implementation class of a broker implements the related interface. On the client I want to cast against this interface, but I get an error. I use the following commands:


IBrokerFactory _factory = (IBrokerFactory)Activator.GetObject(typeof(IBrokerFactory),"tcp://192.168.0.1:8000/FileService.BrokerFactory");
IFileInfoBroker _broker = _factory.GetBroker(typeof(IFileInfoBroker)) as IFileInfoBroker;

I get the factory and the method GetBroker instantiate the correct broker and give it back, but the cast fails on the client and I don't know the reason.

Any hints?

boudino
March 10th, 2005, 02:26 AM
You cannot instantinate an interface like in IBrokerFactory _factory = (IBrokerFactory)Activator.GetObject(typeof(IBrokerFactory),"tcp://192.168.0.1:8000/FileService.BrokerFactory");)

You need concrete class to instantinate. If the class implements an interface, the you can cast to the interface.

torrud
March 10th, 2005, 04:21 AM
You cannot instantinate an interface like in
IBrokerFactory _factory = (IBrokerFactory)Activator.GetObject(typeof(IBrokerFactory),"tcp://192.168.0.1:8000/FileService.BrokerFactory");

Yes I can, because I use Remoting and the brokerfactory will be instantiated on the server from a concrete class and then this object will be send to the client and there that will be casted against the interface. This line of code works. And I can call the methods of the broker factory. That works too, but if I call GetBroker() I get an object from the filebroker class and that object I can not cast against the interface.
It seems to be a mistake with the shared dll or the namespaces or remoting. I can not locate it, but last week it worked and i didn't changed anything at this part of code.

boudino
March 10th, 2005, 08:39 AM
Yes I can, because ...

Yes, you can. I've misread your code. :)

It looks strange. I believe you that _factory.GetBroker(typeof(IFileInfoBroker)) works, althought I still don't understood how :)

What looks your classes like? If you can, post source code of your classes implementig IBrokerFactory and IFileBroker. There are something like attachments on this forum. There must be a solution.

torrud
March 14th, 2005, 08:15 AM
Well, this problem seems to be really tricky. I wrote two little testapplications and called the file service. And it works great, but if I try to insert it in the major project it does not work. And always I get another error message. At the moment I get an invalid cast exception. I think I can not post the whole code from my major application, so it should be impossible to solve the errors by this forum.

But if someone is interested in how to implement an abstract factory I can write a little abstract after solving the problem. That part of code works. :)

Specially thanks to boudino for your helping.