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

Dependency injection design pattern

Normally when designing some parts of the application, one classes is always depends on the other classes. For example, business logic classes (BL) are depend on data access classes (DAL). This dependence can be in very different ways, for example, BL can call static methods of DAL, or create instance of DAL every time it needs it… But in this case it’s can be difficulties 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 Dependency injection pattern. Here is example of how I am using it. Firstly create our classes for DAL:

And for BL:

For parameter in constructor in BL I passed instance of DAL, it’s Constructor injection. And it’s very easy to test this calls with Rhino mocks.

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

Comments

Yaplex - Freelance developer - white logo on black background

Yaplex is an IT Consulting and .NET development company co-founded by freelance developer Alex Shapovalov who lives in Canada and provides software development services in Canada, USA and all over the world.

Contact Information

Toronto, Canada
Phone: +1 647 328 3809
Email: alex@yaplex.com