DEV Community

loading...

Discussion on: Facades should not have Working Plumbing

Collapse
seangwright profile image
Sean G. Wright

What are your thoughts on moving the error handling to a decorating class that wraps the facade? Something dedicated to error handling...

The same could be done for logging or caching to keep cross cutting out of the business logic.

Collapse
integerman profile image
Matt Eland Author

Often what I'll do is either use a private method inside the facade for standard error handling / logging if there's a lot of overlap or use a static class in C# with extension method support so, the class might look like this for logging:

public static ErrorExtensions {

  public void LogError(this Exception ex) {
    // Do some actual error logging here
    Console.WriteLine(ex.Message);
  }

}

and then in my handler I might do:

catch (InvalidOperationException ex) { 
  ex.LogError();
}

If, for some reason, you have multiple facades in an application (can happen if you support different types of external callers, for example), you can extract a base class for all facades and add protected methods for these cross-cutting concerns.

The other thing you could consider is following an Action pattern where you have a single method that does standard stuff like validation and error handling and it just takes in a function that it invokes for the implementation-specific logic.

Forem Open with the Forem app