![]() I do this, because the structure of my code is still likely to change a lot, so extracting an interface and keeping it up to date can be a pain. I often store my dependencies as their concrete types while I'm in the early stages of coding my application. You can store them as their concrete types, but that does tightly couple your dependent class to its dependency (which is generally a bad thing). Now, to answer your question: No, you don't always need to store injected dependencies as interfaces. That's why you tend to see them used together. Like dependency injection, it's a technique for keeping your code loosely coupled. What you're observing is actually called "coding to an interface." Lets say I have a bunch of food items that all implement a IHotFoods interface, and a bunch of drinks that all implement the IHotDrink Interface, would it be okay If I have a meal class that doesn't implement anything and takes in a IHotFood interface and a IHotDrink Interface as arguments into its constructor (that will later be injected by different food/drinks classes ) and then have a meal field in the restaurant class that I can inject with different meal objects? Would this be okay? Or would I have to: Make a meals interface, have meal implement meals, and have Restaurant have a meals interface field that I can inject meal into?Įdit: I'm not asking whether every class should implement an interface, I'm asking whether they should always have interface fields to inject other classes into them that also implement the interface. So basically my question is: Does every field that is going to be injected need to be an interface? ![]() It then goes on by injecting a Tea Class into the Restaurant class's constructor (which takes a IHotDrink interface argument) It then has a third class called Restaurant that has a IHotDrink field inside the class and assigns a value to it in the constructor. ![]() In the tutorial I'm doing it uses interfaces to inject things, for example it uses a IHotDrink interface, then creates a Tea class that implements IHotDrink. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |