1

I'm building a web application and have a service layer for business logic. These services are injected into the controllers. Now, I'm wondering what the best practice is in the following situation.


I have a AuthenticatorService that, unsurprisingly, is responsible for authentication. When a user attempts authenticating, this is clearly something that should be logged. That's something a LoggerService can handle.

Right now, I'm injecting a LoggerService into the AuthenticatorService, but having dependencies between services does not feel very clean...

I'm binding concrete types to interfaces, so logically speaking I would say this is fine - obviously given that it is absolutely certain that the injected service will never have a reason to rely on the other, thus creating a circular dependency.

I'm very interested in reading how others are solving this problem - or, whether it is not a problem at all.

Please do not suggest handling logging from the controller - this is just an example, and there's a lot more situations imaginable where one service might want to call another.

Emile Pels
  • 3,837
  • 1
  • 15
  • 23

1 Answers1

2

I'm very interested in reading how others are solving this problem - or, whether it is not a problem at all.

Yes, it is a problem - in fact, it is one of the most asked about problems. The name of this problem is crosscutting concerns. There are several different ways that the problem could be tackled.

  1. Framework-specific - in MVC/WebApi, there are filters that can run logic in specific controllers, actions, or on every request.
  2. Using a 3rd party Aspect-Oriented Programming (AOP) framework.
  3. Using common design patterns such as proxy or decorator. Here is an proxy example using the .NET RealProxy class.
  4. Interception, a feature built into several major dependency injection frameworks.
NightOwl888
  • 55,572
  • 24
  • 139
  • 212