Webservices with C#, make-up

Asked

Viewed 710 times

3

I need to create a Webservice to export products. Creating Webservico 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 Webservice generico and receive Webservice from Cliente A and of Cliente B at Webservice generico.

  • @Marconi is what I thought I’d do. Have the standard method and then the ones for each customer, making the-for from the customer class to the standard class. But they want to do something Generatic, and as far as I know I would not be able to do this without programming, because I have to have the class ready to generate Webservice. The idea was, customer 3 comes in, puts the structure in a text and generates the new method, but I think this is going to take longer than doing the methods as per demand.

  • @Marlon.tiedt. It clarifies a situation to do this for how many customers, I imagine it is not for many.(more than 10) So it wouldn’t be interesting to profile (add a letter to the client), it wouldn’t be easier to use and maintain.

  • @Luizvichiatto so far is 2 / 3 customers. But my biggest question is, to generate the Webservice for every customer I’m going to have to have the Product, Product, Productob class and so on, where I’m going to have one-to-one from the Product class to the Product class, and the implementations would always be on top of the Product class, and wanted to know if this is valid, or if there would be something more generic to make this implementation.

  • @Marconi, I think I’m going to follow the idea of having the Product class and do the de-stop, and always work with the Product. This way I imagine that I can separate a little the functionalities.

  • Okay let’s delete the comments? Sometimes someone has an answer to this question. But I believe it is broad dmais.

  • I agree with Marconi, this question, as it is formulated, is very broad, so much so that it has a flood of comments. Try to edit the question by restricting your scope a little more.

  • @Felipeavelar edited question, did it became clearer?

  • @Marconi edited question, did it get clearer?

  • @Marlon.Tiedt looked much better. These days I made a purchase in Kanui and another in Saraiva both with the same carrier. In the Webservice tracking link of them was treated one for kanui and another for Saraiva. I believe also be the best option;

Show 4 more comments

2 answers

2

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

if there are only 2 or 3 models, would use polymorphism, if there is a need to add more in the future, would use only one generic class that would have all this configuration done in 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 generate the object xml automatically.

otherwise, would have a relationship table of the fields of products, customers, and values that should be assigned, ex.

Field, Client, new Field code, A, code, product code, B, code, code

then the client would enter all the configuration he needs, and when he returns to the webservice, just read this table and replace the field with the correct value. This way if a new client with a totally different configuration appears, no intervention in the program is necessary.

0

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

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

Another alternative and then address your scenario is to use a fully dynamic API (Link to the post 'Dynamic action Return'), in this case Webapi is the best-case scenario. You will need some prior knowledge of HTTP (as you will need to play the request to generate a compatible Response), which is no big deal. Below follows the link to the last case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.