CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Problems with abstract factory and casting

    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:
    Code:
    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:

    Code:
    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?
    Useful or not? Rate my posting. Thanks.

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Problems with abstract factory and casting

    You cannot instantinate an interface like in
    Code:
    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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Problems with abstract factory and casting

    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.
    Useful or not? Rate my posting. Thanks.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Problems with abstract factory and casting

    Quote Originally Posted by torrud
    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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Problems with abstract factory and casting

    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.
    Useful or not? Rate my posting. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured