vendredi 14 février 2020

Properly handling initialization in generic methods

Suppose I have an object of some type A and want to transform it into type B. Let's say multiple implementations of this interface will exist as we will have many children of A, and B. So, I create an interface like:

interface IAToB {
    B Transform<A, B>(A a);
}

The problem with this interface that I see is I need to initialize an object of type B in Transform, which means if I change the initialization logic of B, I need to appropriately change all implementations of IAToB. So, consider the following:

interface IAToB {
    void Transform<A, B>(A a, out B b);
}

The implementer of IAToB is now required to pass in an initialized B.

So, is the latter interface more compliant with SOLID principles in comparison to the former? Or am I just moving the coupling around without actually reducing it, so to speak? Further, would it make sense to place the generics in the definition of the interface too, to have interface IAToB<A, B> { ... }?

Aucun commentaire:

Enregistrer un commentaire