I was reading a book about Learning ASP.NET Core API when I run to a part saying:
We create a private read-only field _repository that will be assigned the injected MockCommandAPIRepo object in our constructor and used throughout the rest of our code.
Here is some text I thought you'd better have:
Then there are some explanations related to the picture above:
- Add the new using statement to reference ICommandAPIRepo.
- We create a private read-only field _repository that will be assigned the injected MockCommandAPIRepo object in our constructor and used throughout the rest of our code.
- The Class constructor will be called when we want to make use of our Controller.
- At the point when the constructor is called, the DI system will spring into action and inject the required dependency when we ask for an instance of ICommandAPIRepo. This is Constructor Dependency Injection.
- We assign the injected dependency (in this case MockCommandAPIRepo) to our private field (see point 1).
And that’s pretty much it! We can then use _repository to make use of our concrete implementation class, in this case MockCommandAPIRepo. As I’ve stated earlier, we’ll reuse this pattern multiple times through the rest of the tutorial; you’ll also see it everywhere in code in other projects – take note.
Now, According to the highlighted part above in number 2, I got a little confused! I've heard of "to be assigned by some value" before, but here, it is saying that:
that will be assigned the injected MockCommandAPIRepo object in our constructor
and as you see, there is no "by" added before the injected MockCommandAPIRepo object.... So, I have a question now. What does it mean by the highlighted part above in number 2? Does it mean the same when we add "by" in the sentence? or not?