Incorporate IOC (dependency injection) throughout the toolkit #122
StevenRasmussen
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
I agree 100% that introducing DI in the toolkit would be a huge benefit! I've also used MVVM Light in my extensions (which I need to replace with Microsoft's MVVM library). I inject service classes (my own not VS services) into my viewmodel logic a lot. Would it be possible to use the same DI container that Asp.Net Core uses? As you point out many developers already use it. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Just created a POC for this here: #123 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've worked a bit with dependency injection for quite some time now and see some huge benefits if we were to incorporate it into the toolkit. Asp.Net Core has this built-in and so many developers are already familiar with it. The commercial extension that I've built uses it extensively and it works really well. One of the difficulties with the current Visual Studio extension model via the
ProvideService
attribute is that all the services need to be known at design time, which reduces some of the benefits as you can't dynamically register items in the DI container. Perhaps my understanding about this is not quite right though... does anyone know if we have access to the service collection before theIServiceProvider
is built?One area that I think could benefit from this is with regards to commands. Commands will oftentimes take a dependency on other objects or services and so this might require you to pass in arguments to the constructor of the command. If both the command and its dependencies are registered in the DI container then you can simply grab an instance of the command from the container and not have to worry about manually instantiating the object with all of it's dependencies (which may have their own dependencies as well).
If we wanted to incorporate IOC into the toolkit, as I see it we have 2 options:
ProvideService
attribute - this limits us to only being able to register services at design time.ProvideService
attribute.This ties into the discussion around whether or not to make all the major services interfaces (#85). Using DI doesn't "require" per-se that everything is an interface, but it definitely seems to be the "best practice" for purposes of unit testing/mocking etc.
If we were to implement DI, I feel that we'd want all of the services that are currently implemented as properties on the
VS
object to be interfaces and added to the DI container (they all seem to be singletons). We could still keep theVS
paradigm if people want but we'd want the same instance of each interface to be returned whether from one of theVS.*
properties or through the DI container.Adding a DI container could possibly allow us to:
IWindowService.ShowDialog<TWindow>
Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions