mercredi 21 septembre 2022

Design Pattern to combine runtime data with data loaded from a JSON

I have a data class:

public Data
{
    Foo MyFoo
    Bar MyBar
}

Foo is something that needs to a lot of changes and cleaning up to be useful. So I have an interface

public IFooTransformation
{
    Foo Transform(Foo foo)
}

Users create lists of IFooTransformation that are stored in JSON files and loaded at runtime.

foreach (var transformation in transformations)
{
    foo = transformation.Transform(foo);
}

This worked great until now I need to create an AddBarTransformation. All the other transformations include all the properties they need in the JSON, but MyBar is only available at runtime.

Is there a design pattern to help me get MyBar into the AddBarTransformation?

So far I've thinking:

  1. Using a custom JsonConverter that would Set AddBarTransformation's Bar to MyBar.
    • This might work but is a little janky and I haven't figured it out yet.
  2. Changing the interface to accept a Data rather than a Foo
    • This feels bad since the other Transformations only need Foo
  3. Using reflection to loop over the transformations and fill in if any need a Bar.
  4. Separate the logic and data of the IFooTransformation into and use a Builder or a Factory to get the matching logic class while knowing this one needs a Bar passed in.
    • Seems like a lot of complexity when something like 2 is really easy.

Aucun commentaire:

Enregistrer un commentaire