vendredi 27 août 2021

Abstract Factory create objects based on different json format

We need to create a few types of dynamic metadata base on different json format, for example: metadata1

"metadata1": [{
        "item1": "address1",
        "date": "Jan -Jun22",
        "tag": true,
        "itemurl":"item url"
      }]
}

metadata2

{
    "amount": 100,
    "item3": {
        "amount:" 1200,
        "currency": "AUD"
      }
    "item4": [
        "blabla",
        "yaliyada"
      ],
    "flag": true,
    "count": 2
}

And we have those metadata id, I'm thinking the following implementation

public static IMetaData RetrieveMetaData(string metadatDetails, string metadataId)
        {
            switch (metadataId)
            {
                case "metatdata1":
                    return JsonSerializer.Deserialize<MetaData1>(metadatDetails);
                case "metatdata2":
                    return JsonSerializer.Deserialize<MetaData2>(metadatDetails);
                case "metatdata3":
                    return JsonSerializer.Deserialize<MetaData3>(metadatDetails);
                default:
                    return null;
            }
        }

Is there any better implementation using abstract factory? I'm not too familiar with design pattern. I'm thinking to remove switch block, not sure if it's possible. Thanks for advance.

Aucun commentaire:

Enregistrer un commentaire