Dependence Injection and decoupling of program levels

I am trying to implement Dependency Injection to make my tester friendly. I have some pretty serious doubts.

The data layer uses the SqlConnection object to connect to the SQL server database. The SqlConnection object is a dependency for the data access layer. According to the laws of dependency injection, we should not create new () dependent objects, but rather accept them through constructor arguments. Not wanting to upset the IP gods, I faithfully create a constructor in my DAL that accepts SqlConnection.

The business layer calls DAL. Therefore, the business layer must go through SqlConnection. The presentation layer refers to the business layer. Therefore, it must also go to SqlConnection for the business layer.

This is great for isolation and class checking. But aren't we just UI and Business for a specific implementation of the data layer that happens to use the relational database?

Why do presentation and business layers need to know that the underlying data warehouse is SQL? What if the application should support several data sources other than the SQL server (for example, XML files, comma-delimited files, etc.) Also, what if I add another object on which my data level depends from (say, the second database). Now I have to change the top layers to pass this new object.

How can I avoid this carousel and reap all the benefits of CI without pain?

+3
source share
1

, . , Sql SqlConnection.

, SqlImplementation (mock) .

, DI . Sql , SqlConnection.

+3

Source: https://habr.com/ru/post/1747454/


All Articles