Using Microsoft.Extensions.DependencyInjection with MvvmCross can present several challenges and known issues:
1. Incompatibility with MvvmCross Startup Process: MvvmCross requires the ability to resolve components partway through the setup process and then continue registering more components. However, Microsoft.Extensions.DependencyInjection (MS.DI) requires all components to be registered before calling `.Build()` on the service collection. This fundamental difference makes the two systems incompatible without additional workarounds, such as manually translating services from one container to another[1].
2. Two-Stage Startup Process: To leverage MS.DI with MvvmCross, developers often build a MS.DI service collection and then translate its services into the MvvmCross `IMvxIoCProvider` instance. This approach introduces a two-stage startup process, which can be cumbersome and may lead to additional complexity and potential issues[1].
3. Dependency Injection Limitations: MvvmCross's IoC container treats the singleton aspect at the interface level, not the instantiated type. This means if a class implements multiple interfaces, MvvmCross might create separate instances for each interface unless explicitly managed[4]. While this isn't directly related to MS.DI, it highlights the need for careful management when integrating different dependency injection systems.
4. Upgrade and Compatibility Issues: Upgrading MvvmCross versions can sometimes introduce issues with dependency injection, especially when using base view models. For example, changes in constructor selection logic between versions might affect how dependencies are injected[2].
To overcome these challenges, developers often choose to enhance MvvmCross's functionality without replacing its core IoC container. This allows for the use of additional dependency injection frameworks like MS.DI alongside MvvmCross, providing flexibility in choosing the IoC provider[1][5]. However, this requires careful setup and management to avoid conflicts and ensure seamless integration.
In summary, while using Microsoft.Extensions.DependencyInjection with MvvmCross is possible, it requires careful consideration of the startup process, potential incompatibilities, and careful management of dependencies to avoid known issues.
Citations:[1] https://github.com/MvvmCross/MvvmCross/issues/4436
[2] https://github.com/MvvmCross/MvvmCross/issues/4183
[3] https://docs.particular.net/nservicebus/dependency-injection/extensions-dependencyinjection
[4] https://stackoverflow.com/questions/23700121/possible-bug-with-dependency-injection-on-mvvmcross
[5] https://github.com/MvvmCross/MvvmCross/issues/3804
[6] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[7] https://msicc.net/using-microsofts-extensions-dependencyinjection-package-in-xamarin-forms-mvvm-applications-part-2/
[8] https://www.mvvmcross.com/documentation/fundamentals/dependency-injection