Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"using" of two different libraries with almost identical functions

I'm consuming a SOAP web service. The web service designates a separate service URL for each of its customers. I don't know why they do that. All their functions and parameters are technically the same. But if I want to write a program for the service I have to know for each company is it intended. That means for a company called "apple" i have to use the following using statement:

using DMDelivery.apple;

and for the other called "orange"

using DMDelivery.orange;

But I would like to my program to work for all of them and have the name of the company or the service reference point as a parameter.

Update: If I have to write a separate application for each customer then I would have to keep all of them updated with each other with every small change and that would be one heck of an inefficient job as the number of customers increase.

Can anyone think of a solution? I'll be grateful.

like image 235
disasterkid Avatar asked May 06 '13 07:05

disasterkid


People also ask

How do you avoid code duplication?

Don't Repeat Yourself (DRY): Using DRY or Do not Repeat Yourself principle, you make sure that you stay away from duplicate code as often as you can. Rather you replace the duplicate code with abstractions or use data normalization. To reduce duplicity in a function, one can use loops and trees.

How do you avoid code duplication in C++?

The conventional approach to reduce this kind of code duplication is to move the common code to a member function, which can be called from all the constructors. Usually, that member function is called init.

What refactoring can be used when a method is too long has duplicated code?

If the duplicate code is inside a constructor, use Pull Up Constructor Body. If the duplicate code is similar but not completely identical, use Form Template Method. If two methods do the same thing but use different algorithms, select the best algorithm and apply Substitute Algorithm.

How do I find duplication codes?

Duplicate code can be hard to find, especially in a large project. But PMD's Copy/Paste Detector (CPD) can find it for you! CPD works with Java, JSP, C/C++, C#, Go, Kotlin, Ruby, Swift and many more languages. It can be used via command-line, or via an Ant task.


1 Answers

If you have a base contract (interface) for all your services you can use a kind of factory to instantiate your concrete service and only have a reference to your interface in your client code (calling code).

//service interface
public interface IFruitService{
  void SomeOperation();
}

//apple service
public class AppleService : IFruitService{
  public void SomeOperation(){
    //implementation
  }
}

Having for example a kind of factory class (you can put your using statements here)

public static class ServiceFactory{
  public static IFruitService CreateService(string kind){
    if(kind == "apple")
      return new AppleService();
    else if(kind == "orange")
      return new OrangeService();
    else
      return null;
  }
}

And in your calling code (you just add an using statement for the namespace containing your interface):

string fruitKind = //get it from configuration
IFruitService service = ServiceFactory.CreateService( fruitKind );
service.SomeOperation();

You can also use the Dependency Injection principle.

like image 105
polkduran Avatar answered Nov 14 '22 22:11

polkduran