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.
- Public abstract class ProductManagementBLL
- {
- Protected ILogger Logger
- {
- get;
- private set;
- }
- public ProductManagementBLL()
- {
- Logger = NullLogger.Instance;
- }
- public ProductManagementBLL(ILogger logger)
- {
- Logger = logger;
- }
- }
-
-
- public class OrderProcessor: ProductManagementBLL
- {
- public void Execute(Order order)
- {
- try
- {
-
- }
- catch (InvalidOperationException exception)
- {
- Logger.LogError(“exception occurred….. “));
- }
- }
- }
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.