I'm with Nick on this one. If you have a class that is newing up another class, then you have tightly coupled classes. Might as well move the second class into the first one, as you can't have one without the other. If that's how you code, then you might as well put all of your code into one class. I think we can all agree that's not the right way to design your code, but it illustrates my point nicely.
DI is simply pushing the responsibility for where your class is instantiated to somewhere other than where you are using it. Pass the instance in via constructor (or optionally a property) and then (provided you've specified an interface) then you can pass in a real one or test one or even a mocked one. No need to go crazy and us an IoC library if you don't want to (I often don't but MVC core it's built in, so might as well use it). The original example of wrapping up classes into interfaces just to simplify the number of objects being passed in is not something I'd normally do. Difficult to understand which leads to hard to maintain code, even if it does reduce your number of parameters. It would work, I'm sure, as IoC works out all of the dependencies that are nested just fine but you are adding a layer of complexity resulting in things being hidden. Lasagna Code (as opposed to Spaghetti code, layers of code and you can't find the meat) If I come across code not using DI, then I add it. cheers Stephen ________________________________ From: ozdotnet-boun...@ozdotnet.com <ozdotnet-boun...@ozdotnet.com> on behalf of Nick Randolph <n...@builttoroam.com> Sent: Wednesday, 4 December 2019 7:33 AM To: ozDotNet <ozdotnet@ozdotnet.com> Subject: RE: DI in MVC I’m interested in the opinions regarding the use of DI – why is it that you avoid it? I’m asking because I see cases where I find DI invaluable (eg testing) and almost never find that it gets in the way. I’m interested to know whether the opinions are generated based on bad experiences with DI, or a perceived lack of benefits of DI. Nick Randolph | Built to Roam Pty Ltd | Co-Founder, Technical Lead | +61 412 413 425 | 1300 613 140 | www.builttoroam.com<https://www.builttoroam.com/> The information contained in this email is confidential. If you are not the intended recipient, you may not disclose or use the information in this email in any way. Built to Roam Pty Ltd does not guarantee the integrity of any emails or attached files. The views or opinions expressed are the author's own and may not reflect the views or opinions of Built to Roam Pty Ltd. From: ozdotnet-boun...@ozdotnet.com <ozdotnet-boun...@ozdotnet.com> On Behalf Of David Burstin Sent: Wednesday, 4 December 2019 10:19 AM To: ozDotNet <ozdotnet@ozdotnet.com> Subject: Re: DI in MVC Yep. I avoid DI like the plague, but no option in a legacy ASP.net MVC project. Thanks anyway. On Wed, 4 Dec 2019, 09:58 Greg Keogh, <gfke...@gmail.com<mailto:gfke...@gmail.com>> wrote: No takers? Sorry I can't help. I only use DI when I'm forced to, or it's really simply integrated (like in Blazor). I consider DI another form of "magic plumbing" and prefer something closer to the service locator pattern. The problem you're seeing is one I hit when writing Xamarin with FreshMVVM, it became DI spaghetti -- Greg K