WebServices with C #, do de-para

3

I need to create a WebService to export products. Creating the WebService is fine, but depending on the client, I will have to change the name of the elements.

Example, Product Code element:

Generic <codigo>1</codigo><descricao>Produto</descricao>

Client A <codigoProduto>1</codigoProduto><descProduto>Produto</descProduto>

Client B <codProd>1</codProd><desc>Produto</desc>

Will I have to have 1 (one) WebService for each type of structure? Or is there some way to have only the% Web_Service generico and receive the WebService of Cliente A and Cliente B in WebService generico .

    
asked by anonymous 17.07.2015 / 15:45

2 answers

2

Use the polymorphism, or, create a generic class that can be configured according to the client.

If there are only 2 or 3 models, I would use the polymorphism, if there is a need to add more others in the future, I would use only a generic class that would have all this configuration done in a database.

Ex: Polymorphism

public class Produto
{
    public virtual string GetXml()
    {
        return "<codigo>1</codigo><descricao>Produto</descricao>";
    }
}

public class ProdutoClienteA : Produto
{
    public override string GetXml()
    {
        return "<codigoProduto>1</codigoProduto><descProduto>Produto</descProduto>";
    }
}

public class WebService
{
    public string GetProduto(string cliente)
    {
        Produto obj;
        if (cliente=="A")
        {
            obj = new ProdutoClienteA();
        }
        else
        {
            obj = new Produto();
        }

        return obj.GetXml();
    }
}

Remembering that it is just a simple example, there are several other features to implement, including serialization, which would automatically generate the object's xml.

otherwise, it would have a relationship table of the fields of products, clients, and values that should be assigned, eg

Field, Customer, new Field code, A, codeProduct code, B, codProd

there the client would enter all the configuration he needs, and when he returns in the webservice, he just reads this table and replaces the field with the correct value. That way if a new client comes up with a totally different configuration, no intervention in the program is required.

    
18.03.2017 / 20:57
0

Most of the time, there is a static alternative to the issue, just renegotiate the API agreement with the consumer.

When this is not possible, you can choose static structures with dynamic data (Lists with pre-defined objects that have key-values) or even dictionaries.

Another alternative and then addresses your scenario is to use a fully dynamic API ( Link to the post 'Dynamic action return' , in this case WebAPI is the best scenario. You will need some prior knowledge of HTTP (since you will need to reproduce the request to generate a compatible response), which is no big deal. Below is the link to the last case.

    
03.09.2015 / 05:33