I’m in the process of teaching myself the MVVM pattern by dividing the pattern into its core facets and learning those facets one by one.
My question is related to dependency injection. What is it, and why/when should I use it? I’ve looked at Jason Dolinger’s excellent MVVM intro video and I see he uses Unity. This might be strange to ask but how would I implement dependency injection WITHOUT using Unity? I basically want to understand the concept of dependency injection and how to use it without having to implement other frameworks/tools (for now).
Thanks.
I think it’s good that you want to understand DI without using a framework, the concept is not terribly difficult to wrap your head around.
Let’s say you want to use some form of transportation.
An initial implementation of a method that uses a form of transportation might look like this:
The problem with that method is that it is now dependent on a Car class. We should pass our transportation object in for added flexibility. This is inversion of control and is closely related to DI.
As you can see, we don’t need to know anything about a specific DI framework. You might also want to check out the ninject DI by hand tutorial.