Consistent Data Interfaces (Part 1)

Coming from the quick and easy world of duck typed languages like Python or JavaScript, one may first be skeptical of the advantages of type safe languages like C#.

In the former, one may feel the full power of JSON, serializing and deserializing to complex map structures with ease. Higher order functions come with ease, allowing us to compose functions quickly without the many bells and whistles that type safe languages offer.

This can be freeing at times while also introducing the possibility of hard to find type based bugs. Defining a set of unit tests that encompasses all possible states is most helpful in creating safe programs in this sphere.

In the latter we are given the ability to define complex relationships between objects in the hope that we can enforce a consistent and type safe interface. On top of the typical C style type constraints, we are given Generics/Templates, Abstract classes, Virtual functions and Interfaces.

All of these tools give us the power to define consistent systems as we specialized pre-tested functionality into newly composed sub-types. This allows us to rely heavily upon code analyzers to ensure that no errant behavior is introduced into our programs.

As a general example, we can look towards method for decoupling complex objects from various services through Data Transfer Objects and Data Access Objects.

By splitting object up into unrelated entities with decoupled functionalities, we can make our life easier.

First, enforcing object transfer through DTOs allows us not only to standardize the mechanism of transfer between decoupled systems, but also have a single source of truth in finding out what data we are passing across the network.

Second, isolating database interactions in DAOs, we can ensure that the main object’s interface isn’t muddled with database details.

This brings us to the benefits of type safe languages and the expressiveness of being able to constrain generic interfaces.

Abstract classes and interfaces allow us to define compiler enforced requirements upon objects. This in turn, allows us to constrain our problem domain down to a consistent interface that each object that wishes to take part must implement.

Tune in next time for a walkthrough with examples!

Leave a comment

Your email address will not be published. Required fields are marked *