mardi 12 janvier 2016

Correct pattern / approach to avoid / minimize duplicate functionality based on runtime condition

Given this situation:

public const string _constantString_Kind1 = "a constant string";
public const string _constantString_Kind2 = "other constant string";

[SomeSealedAttribute(_constantString_Kind1)]
public static int A_Kind1(); // this was just A()

[SomeSealedAttribute(_constantString_Kind2)]
public static int A_Kind2(); // this was just A() as well,
                             // doing the same thing


public int CallA () {
  return B.SomeRuntimeBooleanFlag ? A_Kind1() : A_Kind2();
}

Is there a way to avoid any of these:

  • declaring A_* twice.
  • a method bootstrapping the call
  • two consts for one runtime check

This seems possible at meta level with T4 templates or using Relefection, but I prefer avoiding those.

Is there some better pattern I can follow to exercise DRY in this scenario?

Aucun commentaire:

Enregistrer un commentaire