Cross Cutting Concepts in a Multilayer Application

The majority of applications we write must have common functionality that spans across layers and tiers. Some of These functionalities are,

  • Authentication
  • Authorization
  • Caching
  • Exception management
  • Logging
  • Instrumentation
  • Validation.
  • Email Notifications

Such functionalities are called as crosscutting concerns because it affects the entire application, and should be centralized in one layer in the code. If we centralize the cross cutting code, we can change the behaviour by changing the code in just one location. Cross cutting concerns increases the modularity and brings easy maintenance in the applications.

How to Design Cross Cutting layer
 
When designing a system we should see the functionalities of the cross cutting codes and how it should behave depending on the layers or whether it is needed to configure it .Sometimes it may require that layers or different tier should use differently to these cross cutting concerns. The dependency injection pattern is the best way when injecting the cross cutting layer code to the layers where we refer these.

Cross-cutting concerns are not the business logic codes these are like utility layer codes which are frequently used in distributed applications and should be separated from business logic.

Example

On a business layer side a base class can have access to a logger factory or logger instance that can be accessed for logging purpose in all derived classes.

  1. Public abstract class ProductManagementBLL    
  2. {    
  3.     Protected ILogger Logger    
  4.     {    
  5.         get;    
  6.         private set;    
  7.     }    
  8.     public ProductManagementBLL()    
  9.     {    
  10.         Logger = NullLogger.Instance;    
  11.     }    
  12.     public ProductManagementBLL(ILogger logger)    
  13.     {    
  14.         Logger = logger;    
  15.     }    
  16. }    
  17.   
  18.   
  19. public class OrderProcessor: ProductManagementBLL    
  20. {    
  21.     public void Execute(Order order)    
  22.     {    
  23.         try    
  24.         {    
  25.             // Business logic code for processing executions of  orders    
  26.         }    
  27.         catch (InvalidOperationException exception)    
  28.         {    
  29.             Logger.LogError(“exception occurred….. “));    
  30.         }    
  31.     }    
  32. }  

The cross cutting codes are injected to the other layers by different ways of injecting (constructor , getter or setter, method ) or using by dependency injection .We can use IOC to provide a pluggable design, and inject the cross cutting concerns as a aspects into different layers of code .These method is known as interceptions in DI containers.

Hope you got some information about cross cutting concepts .Thank you for reading.