Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] Use .NET Generic Host and replace TinyIoC with Microsoft.Extensions.DependencyInjection #313

Closed
berhir opened this issue Jun 11, 2020 · 4 comments

Comments

@berhir
Copy link

berhir commented Jun 11, 2020

I suggest using the .NET Generic Host and replacing TinyIoC with Microsoft.Extensions.DependencyInjection (the link is for ASP.NET Core, but it works the same way with the generic host).

Using the generic host has multiple advantages. Some of them are:

  • It has built-in services for configuration, DI and logging
  • It's easy to use 3rd party DI and logging libraries
  • New .NET features (like IHttpClientFactory) are built on top of the generic host/DI system
  • It's built and supported by Microsoft

James Montemagno wrote already some time ago how to Add ASP.NET Core's Dependency Injection into Xamarin Apps with HostBuilder.

We are using the generic host already together with FreshMvvm in a project, but it was a pain to create a custom IServiceProviderFactory that works with TinyIoC. We found some bugs and many limitations in TinyIoC.
The first plan was to use only M.E.DI and implement the IFreshIOC interface to make it work with FreshMvvm. But then I noticed that FreshMvmm registers and unregisters services after the startup. This is something that is not supported by M.E.DI and many other DI systems.

As a first step, I suggest changing the code to register services only at the startup (and never unregister anything). As far as I see there are only a few places where services get replaced in the container. And there are other ways to accomplish the same.
After these changes, it will be possible (but optional) to use the generic host with M.E.DI and without TinyIoC.

In the long term, IFreshIOC (and TinyIoC) can be removed from this project. I think it's not needed anymore because the generic host has a better default DI system and also better support for 3rd party libraries.

What do you think about it? Do you already have such plans? Will you accept PRs for this?

@libin85
Copy link
Collaborator

libin85 commented Aug 9, 2020

I have been personally thinking on these lines for a while now. So did completely avoid using TinyIOC or did u use a mix of both?

@Brosten
Copy link

Brosten commented Mar 21, 2021

Moving towards the .NET generic host seems like the only reasonable way forward. I would really like to see this happening to FreshMvvm.

@berhir
Copy link
Author

berhir commented Mar 23, 2021

@libin85 sorry, I missed your comment. I wrote a custom implementation of IServiceProviderFactory<FreshTinyIoCContainer> to use the generic host together with TinyIoC. The problem is that FreshMvvm modifies the container after the startup. M.E.DI builds the container once at the startup and then it can't be changed anymore.
My solution works in one of our production apps, but some hacks were needed.

I also think that the generic host and M.E.DI are the way to go for the future. Especially if FreshMvvm wants to support MAUI. And if I remember correctly, FreshMvvm was mentioned to be one of the MVVM frameworks that will support MAUI.

Maybe @rid00z can provide an update on the future of FreshMvvm and MAUI?

@berhir
Copy link
Author

berhir commented Jun 15, 2021

FreshMvvm support for MAUI was confirmed in #325. I am closing this issue because I think it makes no sense to introduce this change in the current version for Xamarin.Forms and for MAUI it will be required anyway.

@berhir berhir closed this as completed Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants