mercredi 5 mai 2021

Is there a more maintainable way to manage software versions than this?

As of now, we are using the below method for object creation in C# (Kind of Factory Pattern). But, as App Versions are growing, these if/else conditions are also growing. Just looking out for more ways or design patterns to write this code in a better way.

Key Points:

  • We have multiple similar methods. i.e. GetAppSettingsHandler, GetDeviceSettingsHandler etc.
  • It is not always when we need to update this code. For example, we did not update our code between release 2.0 and 5.0. So, the current approach does not require change until there is a new handler for a new version.
  • We are using Base class and child classes. Not using the interface. (Historical code)
public static BaseAppSettingsHandler GetAppSettingsHandler()
{
    // Using http header to retrieve app version
    double appVersion = GetCurrentVersion();
    if (appVersion <= 1.5)
        return new A_AppSettingsHandler();
    if (appVersion <= 2.0)
        return new B_AppSettingsHandler();
    if (appVersion <= 5.0)
        return new C_AppSettingsHandler();
    return new BaseAppSettingsHandler();
}

Aucun commentaire:

Enregistrer un commentaire