mardi 19 mai 2020

CQRS - Map Commands from dynamic object

I am using CQRS design pattern with Azure Functions. I am getting dynamic request object through my HttpTrigger Azure Function. Now I have map my commands based on the "EventType" property in the request.

Here is my Azure Function.

[FunctionName("ReceiveEvent")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("ReceiveEvent HTTP trigger function started processing request.");

             log.LogInformation($"Pushing Events to Azure Blob on storage account :-{CloudConfigurationManager.GetSetting("AzureWebJobsStorage")}");

            IActionResult actionResult = null;

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            dynamic data = JsonConvert.DeserializeObject(requestBody);

            var eventType = EventType.Unknown;

            string eventTypeValue = data.EventType;
            Enum.TryParse(eventTypeValue, true, out eventType);



            switch (eventType)
            {
                case EventType.CARD_BLOCK:
                    command = _cardBlockCommandMapper.Map(data);
                    break;
                case EventType.CARD_CANCEL:
                    command = _cardCancelCommandMapper.Map(data);
                    break;


            }

            return actionResult;

Here is my command mapper :

public class CardBlockCommandMapper : ICardBlockCommandMapper
    {
        public CardBlockCommand Map(dynamic data)
        {
            return new CardBlockCommand
            {
                Message = data.Message,
                ModifiedByName = data.ModifiedByName
            };
        }
    }

My question is here, I have total 15 different types of event type. Do I need to create a separate command mapper for each type? Or is there any better way to do this?

Aucun commentaire:

Enregistrer un commentaire