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.
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
- {
- // Business logic code for processing executions of orders
- }
- 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.
Join the conversation! Your thoughts help the community grow.