Combining / converting 2 similar object types from two different APIs

Im a beginner in C # and OOP. Im uses two third-party APIs that contain similar types of objects that have properties that contain the same values, but both APIs have unique (and identical) functions that I need to use. For example:

API1 - point class

General properties

X: Double

Y: Double

Open method

Distance()

ToArray ()

API2 - point class

General properties

X: Double

Y: Double

Open method

Project ()

ToArray ()

I have currently created helper methods for converting from an API1 Point class to an API2 class and vice versa, but there should be a better solution. What will a programming specialist do in this situation? Thanks!

+4
source share
3

.

public class IntergratedPoint{
    // private constructor to prevent misuse
    // If want, you can do a normal constructor which create both pointApi1 and 2
    private IntergratedPoint(){ }

    // this can be set to reference either pointApi1 or 2
    public double X{get;set;} 
    public double Y{get;set;}

    private Api1.Point pointApi1;
    private Api2.Point pointApi2;

    public static explicit operator IntegratedPoint(Api1.Point pointApi1){
        IntegratedPoint newPoint = new IntegratedPoint();
        newPoint.pointApi1 = pointApi1;
        newPoint.pointApi2 = new Api1.Point();
        // set X and Y for pointApi2
    }

    // the explicit operator for Api2.Point

    public double Distance(){
        return pointApi1.Distance();
    }
    public double Project(){
        return pointApi2.Project();
    }
    public double[] ToArray(){
        // don't know what to do, but it you can do either pointApi1.ToArray() or so
    }    
}
0

Automapper.

Mapper.CreateMap<Order, OrderDto>();

OrderDto dto = Mapper.Map<OrderDto>(order);
0

As a result, I added various extension methods to the API1 Point class. Using type conversion hedging methods, I can use the API1 class class of API2 class. With this, I only use API1 objects in my code.

0
source

All Articles