views:

61

answers:

2

I've got a .NET Web Service Reference to a 3rd party WSDL.

In that reference are 2 classes. Basically these 2 classes are most likely Interfaces on the 3rd Party API side but in .NET end up as 2 proxy classes.

I have a need to combine both these classes into one class. Why? Because it's stupid that these are split, they're the service which allows me to make method calls. The method calls are all split half and half between these 2 proxy classes.

So I want to create a custom wrapper class called ThirdPartyService and somehow essentially inherit both those proxy class's members. I know you can inherit 2 classes in C# but I don't see how to do this with an interface either.

+3  A: 

Well, you haven't given us a lot to work with, but you could do this using composition.

class Foo {
    public void Frobber();
}

class Bar {
    public void Blorb();
}

Then

class FooAndBar {
    Foo _foo;
    Bar _bar;
    public FooAndBar(Foo foo, Bar bar) {
        _foo = foo;
        _bar = bar;
    }

    public void Frobber() { _foo.Frobber(); }
    public void Blorb() { _bar.Blorb(); }
}

If Foo and Bar implement interfaces IFoo and IBar respectively then you can have FooAndBar implement IFoo and IBar too.

In the language of Design Patterns we would call this the adapter pattern.

Regarding your edit:

I know you can inherit 2 classes in C# but I don't see how to do this with an interface either.

No. You can not inherit from two classes in C#. You have to use composition like I already explained.

Jason
Resharper has an option to generate delegating members to simplify this type of thing. Very useful.
Sam
That looks about right. Maybe he was thinking that Foo and Bar implement IFoo and IBar, so FooAndBar would likewise implement both.
Steven Sudit
I used ServiceA, B and Z and "DoThis" and "DoThat" but yeah, that's what I was gonna say :)
flq
Didn't know Resharper did that. Nice feature.
Steven Sudit
updated my original post with more details.
CoffeeAddict
@Frank: Always `Foo` and `Bar`. Always. http://en.wikipedia.org/wiki/Foobar
Jason
Steven read the updated post. They are 2 proxy methods, not interfaces that I need to combine into one custom class of mine.
CoffeeAddict
@coffeeaddict: Great. My answer still applies. You have to wrap the two classes into a class of your own and delegate the methods appropriately. As @Sam mentions, Resharper can make this easy for you. Otherwise just do as I have done.
Jason
in this case, there are no interfaces I can use...
CoffeeAddict
@coffeeaddict: I don't understand your last comment. Is that a question or a statement? Please help me help you.
Jason
it's a statement.
CoffeeAddict
@coffeeaddict: You don't need an interface to wrap two classes into one using composition.
Jason
Ok, so you have a master class that exposes 2 property instances. I don't see though when the caller calls FooAndBar that the caller is able to do FooAndBar instance = new FooAndBar(); and then instance.SomeMethod where SomeMethod is either in Foo or Bar but the caller doesn't know and doesn't need to know becuase I've combined the method signatures between both Foo and Bar. I don't see how yours lets me do that. It seems to me I still need to know whether the method is in Foo OR Bar and this is what I'm trying to keep my caller form having to do (ignorant of this)
CoffeeAddict
@coffeeaddict: First, I did not expose any property instances. Note that both `_foo` and `_bar` are private (they are not marked as `private` but the default access modifier is `private` in C#). Second, the caller does not need to know where the method they are invoking is being delegated to. They can just call `Frobber` or `Blorb` as they need and the class `FooAndBar` will delegate appropriately. Third, if you don't want the user to have to pass in instances of `Foo` and `Bar` it's easy enough to modify the constructor accordingly.
Jason
gotcha. I was just hoping for a way not to have to build out all those method wrappers...looks like there is no way to sort of inherit the method signatures so you don't have to manually type wrappers / delegates for them. Thanks.
CoffeeAddict
@coffeeaddict: No, but as mentioned there are tools (such as Resharper) that can help.
Jason
A: 

Maybe to add some value to Jason's post, this sort of stuff would also be known as adapter

flq
I guess I was hoping for a very straight forward answer to my problem. Doesn't look like there is one.
CoffeeAddict