mardi 6 décembre 2022

Interfaces design best practices (separate interfaces vs inheritance)

I have an element with complex id (consists from 2 Guids) and I need to represent an element with two interfaces (interface of the element id and interface of the element itself, with it's properties), so, I have two options for it:

Option #1:

interface IElementId {
    Guid Id1;
    Guid Id2;
}

interface IElement : IElementId {
    string Name;
    string Type;
    ...etc
}

The key point here - both ids (Guids) are part of the element itself, so IElement "includes" IElementId in itself by inheritance.

Option #2:

interface IElementId {
    Guid Id1;
    Guid Id2;
}

interface IElement {
    IElementId Id;
    string Name;
    string Type;
    ...etc
}

The point here - Id is a separate property represented by the separate interface and is included in the element as property of the IElementId interface type.

It is not easy to find best desing practices and advices for this case, so, please, feel free and be welcome to share your opinion about option #1 vs option #2.

Aucun commentaire:

Enregistrer un commentaire