1

I'm new to the SOLID principles. I have this scenario:

class ErrorClass {

   constructor(name: string, description: string) {
      // 
   }

}

class Class1 {

   public someMethod() {
         try {
            // good scenario
         } catch (error) {
            throw new ErrorClass("Error Name","Error Description");
         }
   }

}

Is that wrong? Am I allowed to call ErrorClass directly from Class1 or am I supposed to inject ErrorClass into Class1 as a dependency? I'm worried that my Class1 constructor can get bloated if I have to inject different error classes along with other classes it might also depend on?

Grateful for any help or advice!

Thanks

4

1 回答 1

1

I would suggest you read upon Cross-cutting concern. Here are a few links:

  1. https://en.wikipedia.org/wiki/Cross-cutting_concern
  2. https://www.c-sharpcorner.com/UploadFile/vendettamit/managing-cross-cutting-concerns-the-logger-and-logging/

Please note, that in most cases they give example of a Logger class as a Cross-cutting concern - a logger is something that every class needs, and according to the SOLID principles it should be injected as a ctor parameter to every class, right? Wrong. Logger class is an exception to the SOLID principles because it is a Cross-cutting concern.

And in your case, your ErrorClass is also a Cross-cutting concern. Therefore, it should be a global static and not injected as a parameter to ctor.

于 2021-10-28T12:52:18.697 回答