Inversion of control in 3 layer architecture
While designing layered application we typically follow following structure:
In this traditional approach UI layer refers to Business access layer(BAL/Controller layer) and BAL refers to Data access layer (DAL). This referring is usually hard coded at each layer leading to numerous problems simply because of the reason that we don't program to abstraction. The problem which could crop up are:
In order the remove these hard coded dependencies we should invert our layering structure and code to abstractions.
We can inject dependencies at business access layer so that we can interchange/fake our data access layer at any time. Constructors/properties/method dependency injection can be used. To make it more extensible dependency can be specified in the config using different containers like unity.
So the architecture after complete overhaul will look like
In this architecture BAL uses data access layer abstractions and dependencies are injected in the BAL for corresponding DAL. This way our interchange our DAL later and can also test it by faking.
There is more on inversion of control in the later posts!!!
In this traditional approach UI layer refers to Business access layer(BAL/Controller layer) and BAL refers to Data access layer (DAL). This referring is usually hard coded at each layer leading to numerous problems simply because of the reason that we don't program to abstraction. The problem which could crop up are:
- Data access layer is not interchangeable. Suppose in future in stead of storing data to data we choose flat file storage, so in order to accommodate this in current layering BAL and DAL both has to rebuilt.
- Its not easy to test the whole system without each and every component up and running. In short its not east the fake the data layer or controller layer to test the same using automated tests.
- Its not easy to share the business rules across application or modules as they are dependent on the lower layers directly. Business rules are generally shared across all the systems despite the underlying layer and the layer using it.
In order the remove these hard coded dependencies we should invert our layering structure and code to abstractions.
We can inject dependencies at business access layer so that we can interchange/fake our data access layer at any time. Constructors/properties/method dependency injection can be used. To make it more extensible dependency can be specified in the config using different containers like unity.
So the architecture after complete overhaul will look like
In this architecture BAL uses data access layer abstractions and dependencies are injected in the BAL for corresponding DAL. This way our interchange our DAL later and can also test it by faking.
There is more on inversion of control in the later posts!!!


Comments
Post a Comment