What to go through the constructor and what to go through the interface?

This is a question about dependency injection. When creating the service object, we pass the co-authors through the constructor at the construction stage. The service object will implement the interface that will be called during the execution phase.

It is sometimes difficult to understand whether a particular object should go through the constructor or be part of the interface implemented by the service class?

Are there any rules for choosing one option over another? The question is more complicated if you know that the interface will be called only once in the script that you are encoding.

+3
source share
2 answers

I like to think like this:

  • Constructor arguments are implementation details
    • ()
    • , .
    • .
    • , .

. , : " ". :

void Insert(User user);

void Insert(User user, IDbConnection dbConnection);

:

:

:

" ". , , :

void Insert(User user);

.

+5

, , , . , . , -, , , , .

+2

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


All Articles