concept auditing in category dependency injection

This is an excerpt from Manning's book Dependency Injection.
Such abilities of Interception move us along the path towards Aspect-Oriented Programming (AOP), a closely related topic that we’ll cover in chapters 10 and 11. With Interception and AOP, you can apply Cross-Cutting Concerns such as logging, auditing, access control, validation, and so forth in a well-structured manner that lets you maintain Separation of Concerns.
9.1.2 Example: Implementing auditing using a Decorator
In this example, we’ll implement auditing for the
IUserRepositoryagain. As you might recall, we discussed auditing in section 6.3, where we used it as an example when explaining how to fix Dependency cycles. With auditing, you record all of the important actions users make in a system for later analysis.Auditing is a common example of a Cross-Cutting Concern: it may be required, but the core functionality of reading and editing users shouldn’t be affected by auditing. This is exactly what we did in section 6.3. Because we injected the
IAuditTrailAppenderinterface into theSqlUserRepositoryitself, we forced it to know about and to implement auditing. This is a Single Responsibility Principle violation. The Single Responsibility Principle suggests that we shouldn’t letSqlUserRepositoryimplement auditing; given this, using a Decorator is a better alternative.
Listing 9.2 Implementing
AuditingUserRepositoryDecoratorpublic User GetById(Guid id) #1 { #1 return this.decoratee.GetById(id); #1 } #1 public void Update(User user) #2 { #2 this.decoratee.Update(user); #2 this.appender.Append(user); #2 } #2 #1 Omits auditing on read operations #2 Write operation decorated with auditingNot all operations need auditing. A common requirement is to audit all create, update, and delete operations, while ignoring read operations. Because the
GetByIdmethod is a pure read operation, you delegate the call to the decorated Repository and immediately return the result. TheUpdatemethod, on the other hand, must be audited. You still delegate the implementation to the decorated Repository, but after the delegated method returns successfully, you use the injectedIAuditTrailAppenderto audit the operation.
Figure 10.4 Enriching a real command service with auditing, transaction, and security aspects
![]()