Matthijs Wessels
Matthijs Wessels

Reputation: 6739

How do I make DRY code using generated WCF proxies from overlapping WSDLs

I have a bunch of WSDLs from our customer describing webservices that I have to use in my application.

There is one webservice for creating items, one for querying them and one for updating them. These WSDLs have a lot of overlap. The items I have to create, query and update are present in all three WSDLs in the same way.

When I use svcutil, I can generate proxies for the three WSDLs, but then every proxy defines the items again. So I have the same class, three times, only in a different namespace.

Working with this forces me to duplicate a lot of code (e.g. mapping a domein object on a proxy class has to be done for every proxy in exactly the same way).

The only solution I can think of is to manually edit the proxies and take out the overlap. However, I think that will make it difficult to manage changes in the web interfaces when I regenerate the proxies.

I wonder if it is possible to somehow generate all the proxies in one go.

Thanks in advance.

Upvotes: 2

Views: 124

Answers (1)

Brady
Brady

Reputation: 10357

You might consider writing a program that parses all the customer WSDLs and stores the operations, etc in a map (or multimap) of some sort. Then iterate over the map and remove the duplicates and write the result to another, final WSDL and codegen on this one. You'll have to do something with the namespaces though.

Something to consider though: would the code needed to do so be more than the extra code you're already duplicating?

Another option could be to define a Proxy class for the duplicated operations. You would have to initialize it by adding the operations with the namespace (create a map of namespace strings to a map of operations, which itself would be the operation strings mapped to the code/object to execute). Then each time you want to perform a specific operation, pass in the namespace string to operate on.

Imagine we had Op1 defined in namespaces ns1, ns2, and ns3 and Op2 in ns1 and ns2. Then the pseudo-code (strongly c++ biased) could be something like:

class ProxyOperations
{
    addOperation(string opName, string namespace, OperationBase op);
    // You'll have to figure out what to do with the operation parameters,
    // maybe wrap them in a parameter context
    void doOperation(string opName, string namespace) {
        // lookup operation object and execute it
    }
    typedef map<string, OperationsBase> OperMap;
    typedef map<string, operMap> NamespaceMap;
    NamespaceMap namespaceOperationMap_;
};

{
    ProxyOperations po;

    // Populate it
    po.addOperation("Op1", "ns1", oper1ObjectNs1);
    po.addOperation("Op1", "ns2", oper1ObjectNs2);
    po.addOperation("Op1", "ns3", oper1ObjectNs3);

    po.addOperation("Op2", "ns1", oper2ObjectNs1);
    po.addOperation("Op2", "ns2", oper2ObjectNs2);

    // now use it
    po.doOperation("Op1", "ns2");
}

Upvotes: 1

Related Questions