The only thing I feel like this explanation needs is, what if I want to have both an Orc and a Goblin enemy at the same time? Besides that it is a great intro to Dependency Injection. But I feel like this is what most dependency injection intro's forget about. It is nice to be able to swap out which service is used in compile time. But with interfaces often you want to decide this during runtime no?
Thanks so much for the feedback! You’re totally right. It’s not just about swapping services at compile time. If you want both an Orc and a Goblin at the same time, you'd need a way to handle that at runtime, like using a factory. I might dive deeper into this in a follow-up video. Appreciate you pointing it out!
@@robadobdob Personally I find this a terrible advice. The Microsoft DI implementation is terrible and keyed services are an anti pattern. The only time you can use this, is if you really want to have 2 instances of the same interface. Even if that is the case, I prefer 2 dummy derived interfaces over having magic strings in my application. It is a nice getting started with DI vid, but the creator has picked not a good example raising to this question. A better design, in my opinion, is to change the interface from IDamageCalculator to IDamageCalculator
Never use new()? Bit hyperbolic. You don’t need DI for simple, throw-away, or POC projects. Your example is better off using a factory as it’s simple and doesn’t need to manage object lifecycle. Plus you’d probably want to dynamically choose your implementation at runtime and not hard code it in your DI registration. You should explain when to use DI/IoC and when to use the factory pattern as this example didn’t need scoping or lifecycle management (e.g. disposal) and is a better example of using a factory. Also would be good to contrast DI vs factory pattern vs mediator pattern. Your example could also use a mediator and be better off than DI although it would incur more overhead than a factory and be better for complex systems with complex dependency graphs.
So this serves as a simple explanation of DI including a basic demonstration, but i'm not sure it actually explains why you would want to. Use of a REST API in the demonstration confuses things somewhat as you would expect methods on an EnemyController to receive an IEnemy on which to act rather than having one injected on controller construction. I know it was only used as an easy way to demonstrate the concept but I feel like is raises more questions than it answers.
Usaully love Patrick's videos but I agree with you on this one. The example needs to be a deeper and I think cover an actual requirement vs. some made up RPG cruft.
Pretty much. Makes the whole thing clear as mud because it got lost in the sauce. As a DI example, this would have gone into a Factory pattern that would then be used by "whatever you're doing", but instead it got thrown into the .Net API internals and it becomes "what are we talking about"? A better example would be a controller that got a "creature type", goes into a factory and gets an IEnemy for that type. And then have something like "Difficulty [Easy|Normal|Hard]" as a service injection. Now the controller can return enemy.Attack()*Difficulty(). Voila, everything is injected and the example makes sense...
Idk. Maybe to switch datasources or different logic to calculate something for different countries because of different rules and laws. But yes, I do not got the point either.
Timestemp 4:10 The question for me is: What actually happens when someone creates an Ork object? With this dependency injection also creates a DamageCalculator, right?
I feel the explanation is great but the payoff at the end falls flat, I've seen this with a lot of tutorials, at the end it's not clear how this will be used.
I had a job interview recently and almost failed it 'cause got confused with services life-time, after your video this topic has become clear and I will never fail that question again😂
The only thing I feel like this explanation needs is, what if I want to have both an Orc and a Goblin enemy at the same time?
Besides that it is a great intro to Dependency Injection. But I feel like this is what most dependency injection intro's forget about. It is nice to be able to swap out which service is used in compile time.
But with interfaces often you want to decide this during runtime no?
Thanks so much for the feedback! You’re totally right. It’s not just about swapping services at compile time. If you want both an Orc and a Goblin at the same time, you'd need a way to handle that at runtime, like using a factory. I might dive deeper into this in a follow-up video. Appreciate you pointing it out!
I think that’s where the new Keyed services comes in. You can register both with a different key and then select at runtime by key.
@@robadobdob Personally I find this a terrible advice. The Microsoft DI implementation is terrible and keyed services are an anti pattern. The only time you can use this, is if you really want to have 2 instances of the same interface. Even if that is the case, I prefer 2 dummy derived interfaces over having magic strings in my application.
It is a nice getting started with DI vid, but the creator has picked not a good example raising to this question.
A better design, in my opinion, is to change the interface from IDamageCalculator to IDamageCalculator
@@argamon2025 I never said it was a good or bad pattern, just that it exists and it might answer the question.
Factory or in more complex setups, the mediator pattern.
Never use new()? Bit hyperbolic. You don’t need DI for simple, throw-away, or POC projects.
Your example is better off using a factory as it’s simple and doesn’t need to manage object lifecycle. Plus you’d probably want to dynamically choose your implementation at runtime and not hard code it in your DI registration.
You should explain when to use DI/IoC and when to use the factory pattern as this example didn’t need scoping or lifecycle management (e.g. disposal) and is a better example of using a factory.
Also would be good to contrast DI vs factory pattern vs mediator pattern. Your example could also use a mediator and be better off than DI although it would incur more overhead than a factory and be better for complex systems with complex dependency graphs.
So this serves as a simple explanation of DI including a basic demonstration, but i'm not sure it actually explains why you would want to. Use of a REST API in the demonstration confuses things somewhat as you would expect methods on an EnemyController to receive an IEnemy on which to act rather than having one injected on controller construction. I know it was only used as an easy way to demonstrate the concept but I feel like is raises more questions than it answers.
Usaully love Patrick's videos but I agree with you on this one. The example needs to be a deeper and I think cover an actual requirement vs. some made up RPG cruft.
Pretty much. Makes the whole thing clear as mud because it got lost in the sauce. As a DI example, this would have gone into a Factory pattern that would then be used by "whatever you're doing", but instead it got thrown into the .Net API internals and it becomes "what are we talking about"? A better example would be a controller that got a "creature type", goes into a factory and gets an IEnemy for that type. And then have something like "Difficulty [Easy|Normal|Hard]" as a service injection. Now the controller can return enemy.Attack()*Difficulty(). Voila, everything is injected and the example makes sense...
But in this case why would i want have goblin and orc at the same scope of project? because to use one i need to register and unregister the other
Idk. Maybe to switch datasources or different logic to calculate something for different countries because of different rules and laws. But yes, I do not got the point either.
Timestemp 4:10
The question for me is: What actually happens when someone creates an Ork object? With this dependency injection also creates a DamageCalculator, right?
I feel the explanation is great but the payoff at the end falls flat, I've seen this with a lot of tutorials, at the end it's not clear how this will be used.
I had a job interview recently and almost failed it 'cause got confused with services life-time, after your video this topic has become clear and I will never fail that question again😂
Hi patrick,
Can you make one video on Iqueryable and Ienumarable.?
Hey are you looking for a Video Editor?
Hey Patrick, i applied for your job. Your description says I should contact you here.
thank you so much
Great explanation
Glad you think so! Thanks a lot for your feedback!
😂 di is test ? wow kui kui