• Home /
  • Blog /
  • Dependency injection design pattern
Dependency Injection Design Pattern

Dependency injection design pattern

Usually, when designing some parts of the application, one class always depends on other classes. For example, business logic classes (BL) depend on data access classes (DAL). This dependence can be in very different ways; for instance, BL can call static methods of DAL, or create an instance of DAL every time it needs it… But in this case, it can be challenging to test business logic with Unit tests because it’s difficult or even impossible to create mocks to the database. To solve this problem, you can use a Dependency injection pattern. Here is an example of how I am using it. Firstly create our classes for DAL:

And for BL:

For a parameter in the constructor in BL I passed an instance of DAL; it’s Constructor injection. And it’s straightforward to test these calls with Rhino mocks.

Some developers can say than Dependency Injection is a not good pattern for the database layer, and there are some other patterns for this purpose. Still, I like this solution and successfully using it.

Yaplex - Freelance developer - white logo on black background

Yaplex ® is a custom software development company located in Mississauga, Ontario. Yaplex was founded in 2013 and provides website development, mobile development, and custom .NET software development services.

Contact Information

Mississauga, ON, Canada
Phone: +1 647 328 3809
Email: hello@yaplex.com