jeudi 24 novembre 2022

What would be the best way to simplify/refactor this because it is getting bloated :(

There is some decision logic in the software that I'm currently working on which is getting very unmaintainable and bloated. What would be the best way to refactor this? Is there some specific design pattern that could be used here?

    public Task Handle(
    OrderState orderState,
    OrderType orderType,
    bool isInternal,
    int clientId
    )
{
    return (orderState, orderType, isInternal) switch
    {
        (OrderState.New, OrderType.Personal, true) => GenericHandler(clientId, orderType, isInternal),
        (OrderState.Confirmed, OrderType.Personal, false) => OrderCreated(clientId, orderType, isInternal),
        (OrderState.New, OrderType.Corporate, true) => GenericHandlerCorporate(clientId, isInternal),
        (OrderState.BeingPrepared, OrderType.Personal, true) => GenericHandler(clientId, orderType, isInternal),
        (var type, OrderType.Corporate, false) when IsInternalWhitelisted(clientId) =>  OrderWhitelistedCreated(clientId, orderType, isInternal),
        (OrderState.Delivered, OrderType.Corporate, true) => GenericHandlerCorporate(clientId, isInternal),
        (OrderState.BeingPrepared, OrderType.Corporate, true) => GenericHandler(clientId, orderType, isInternal),
        (OrderState.Delivered, OrderType.Personal, false) => OrderCreated(clientId, orderType, isInternal),
        (OrderState.Confirmed, OrderType.Corporate, true) => GenericHandlerCorporate(clientId, isInternal),
        _ => ((Func<Task>)(() =>
        {
            DoSomethingElse();
            return Task.CompletedTask;
        }))()
    };
}

There are a lot more combinations in the switch but have been removed for keeping code snippet small.

I was thinking of maybe creating some sort of a map with all the possible combinations in it and somehow going from there.

Aucun commentaire:

Enregistrer un commentaire