ooparchitecturesoftware-designreadabilitycode-complexity

How to handle a wide variety of object types for the same data


I am building an application which uses a lot of vectors and for which I am using a lot of third party and system assemblies. The result of this is that I have four ways to to represent 3d double precision vectors, and five ways to represent collections of those vectors. This is true for just about everything I have. The result is that I need to handle 30 different types for about three different actual structures. From your experience, what is the cleanest way to handle this?


Solution

  • If the other systems are providing vector data to your application, and the transfer is just one-way (into your app), then all you have to do is decide how you want to store / handle vectors in your app, and when data comes in simply convert it to that format. The conversion is a one-time thing, and once it's converted you only have the one format to worry about.

    (The same architecture can handle two-way interactions - let me cover that last).

    This is where Dependency Inversion comes in handy, because you can write different modules where each one is dedicated to work with a specific 3rd party system, and load up the appropriate one at runtime.

    enter image description here

    Diagram / architecture explained:

    1. External Vector Service - the sub-system or layer that handles the incoming data from the 3rd party systems.
    2. Service Logic - the code that handles / coordinates what the External Vector Service does.
    3. IExternalVectorProvider an interface that describes the operations you need to support, e.g. "SaveFunkyVectors", "SaveDoubleProjectionVector".
    4. SystemA_VectorProvider, SystemB_VectorProvider and SystemC_VectorProvider - the 3rd party specific classes that implement IExternalVectorProvider.
    5. Dependency Injection Sub-System - at runtime, loads the right IExternalVectorProvider implementation into the Service Logic.

    So for example:

    I put the dependency injection in the main part of the app because ideally it'll be generic and you can reuse it elsewhere.

    What about Bi-Directional?

    The exact same architecture can be used - as long as your IExternalVectorProvider set-up can re-format data from your standard back to the target 3rd party one.