jeudi 3 novembre 2016

Is there an alternative to the Notification pattern for multiple messages and success/failure?

Is there an alternative to the Notification pattern for multiple messages and success/failure?

I have a class, OperationResult, that I use to return a Success boolean and a list of "error" messages. These messages are sometimes unexpected errors but more often ordinary cases that often happen. Sometimes we return single error messages but other times we return several.

This seems to be more or less the Notification pattern advocated by Fowler: http://ift.tt/1D6FHXQ. The consumers then do something reasonable with the success state, most often displaying errors to the user but sometimes continuing on in the case of non-fatal errors.

I thus have lots of service methods (not web service methods) that look something like this:

private ThingRepository _repository;
public OperationResult Update(MyThing thing)
{
    var result = new OperationResult() { Success = true };

    if (thing == null) return result;

    OtherThing original = _repository.GetOtherThing(thing.originalId);
    if (original == null) return result;

    if (AnyPropertyDiffers(thing, original))
    {
        result.Merge(UpdateThing(thing, original));
    }

    if (result.Success) result.Merge(UpdateThingChildren(thing));
    if (!result.HasChanges) return result;

    OperationResult recalcResult = _repository.Recalculate(thing);

    if (recalcResult.Success) return result;

    result.AddErrors(recalcResult.Errors);
    return result;
}
private OperationResult UpdateThing(MyThing ) {...}
private OperationResult UpdateThingChildren(MyThing) {...}
private bool AnyPropertyDiffers(MyThing, OtherThing) {...}

As you can imagine, UpdateThing, UpdateThingChildren, and ThingRepository.Recalculate all have similar OperationResult merging/manipulating code interleaved with their business logic.

Is there an alternative to so much code munging around my returned object? I'd like my code to just focus on the business logic without having to be so particular about manipulating an OperationResult.

Note: throwing exceptions isn't really appropriate here as the messages aren't exceptional.

Aucun commentaire:

Enregistrer un commentaire