-3

We need to call a webservice which registers a shipment with either UPS or TNT. You fill in some details, call the function to register and pass json for details. Pretty basic stuff.

Shipment shipment = new Shipment
{
    Services = new List<int>(),
    Lines = new List<Line>()
};

shipment.Addresses = new List<Carrier.Address> {
    new Carrier.Address {
        Name1 = dealer.Name,
        Street1 = dealer.Address, // etc
    }
};

HttpResponseMessage response = mClient.Client.PostAsync(client.BaseAddress, shipment.ToJSON()).Result;

This worked fine with UPS but when using TNT an error was returned.

Some more values need to be sent for TNT to accept the order but not for UPS.

shipment.DetailGroups = new List<DetailGroup>
{
    new DetailGroup
    {
        GroupID = 1,
        new Row
        {
            Details = new List<Detail>
            {
                new Detail
                {
                    KindID = 1,
                    Value = 1
                },
            }
        }
    }
};

Now instead of having a huge switch statement for every shipment API, what is a good adapter to use in this situation? I was thinking adapter but that one seems more for unifying different interfaces.

Peter Csala
  • 489
  • 1
  • 3
  • 10
  • Does this answer your question? [Choosing the right Design Pattern](https://softwareengineering.stackexchange.com/questions/227868/choosing-the-right-design-pattern) – gnat Oct 12 '20 at 06:04
  • @ServeLaurijssen Please try to avoid to call `.Result` on an async method. Please prefer `await` or as a last result: `GetAwaiter().GetResult` [Discussion about the difference between the two](https://stackoverflow.com/questions/17284517/is-task-result-the-same-as-getawaiter-getresult). – Peter Csala Oct 12 '20 at 06:48

2 Answers2

1

You could create two different shipment classes:

class TNTShipment
{
   // Contains Services, Lines and Addresses
}

class UPSShipment
{
   // Contains Services, Lines, Addresses and DetailsGroups
}

You can use regular OO techniques to avoid code duplication.

Create the correct instance and serialize it when sending it to the webservice.

Rik D
  • 3,806
  • 2
  • 15
  • 26
0

So first off. Some data is the same: But can be mutable. If you know for sure, you only need UPS or TNT, then you technically only need to adjust your data mapping for those two carriers.

However, that you are using 2 would indicate, you would eventually like to expand to more than those two as well.

In that case, I would create "connectors" that handle processing the specific requests to and from the API(web-service) specifically, and nothing else.

Then use a queuing system like Kafka, Rabbitmq or whatever, to process these connector results into a generic format, your internal system is in control over, you BI, if you will.

This will allow you to parse and control data from multiple sources into a single format, so you don't need to allocate "specializations" across your entire system, but only at the "connector" level.

You, of course, don't need to use a microservice architecture, especially if you are far away from scaling issues, but having your software designed to hit a persistence layer that can be altered for any source to a queue system, is likely what you should be doing.

Morten Bork
  • 101
  • 1