concept auditing in category dependency injection

appears as: uditing, Auditing
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 IUserRepository again. 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 IAuditTrailAppender interface into the SqlUserRepository itself, 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 let SqlUserRepository implement auditing; given this, using a Decorator is a better alternative.

Listing 9.2 Implementing AuditingUserRepositoryDecorator
public 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 auditing

Not all operations need auditing. A common requirement is to audit all create, update, and delete operations, while ignoring read operations. Because the GetById method is a pure read operation, you delegate the call to the decorated Repository and immediately return the result. The Update method, 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 injected IAuditTrailAppender to audit the operation.

Figure 10.4 Enriching a real command service with auditing, transaction, and security aspects

10-04.eps