Git Product home page Git Product logo

maui-configure-ioc-sample's Introduction

Configuring an IoC Container in a MAUI app

I've always been a fan of MvvmLight's SimpleIoc and was really pleased to see it mature and make its way into the CommunityToolkit after MvvmLight's deprecation.

In Marco Siccardi's blog post "Make the IServiceProvider of your MAUI application accessible with the MVVM CommunityToolkit" he walks through how this Container can be configured by extending the Maui App's Application class and calling Ioc.Default.ConfigureServices() from the construct of the resulting sub-class.

As highlighted in Marco's post, there are many ways to achieve this, however Sub-classing Application seemed a little too hard work for me :)

Alternative Approach

The IServiceProvider is exposed from the MauiApp object created in the MauiProgram.CreateMauiApp() method.

Rather than sub-class, we can simply extend the last line of CreateMauiApp():

MauiProgram.cs

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

        builder.Services.AddTransient<MainViewModel>();

        var mauiApp = builder.Build();
        Ioc.Default.ConfigureServices(mauiApp.Services);
        return mauiApp;
    }    
}

A cleaner approach would be to create an extension method, allowing us to keep it to a single line and configure the IoC Container elsewhere.

MauiAppExtensions.cs

internal static class MauiAppExtensions
{
    public static MauiApp ConfigureIoc(this MauiApp mauiApp)
    {
        Ioc.Default.ConfigureServices(mauiApp.Services);
        return mauiApp;
    }
}

MauiProgram.cs

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        ...

        builder.Services.AddTransient<MainViewModel>();

        return builder.Build().ConfigureIoc();
    }    
}

Thanks again to Marco for the inspiration, this is in no way intended to put down his approach, only to highlight that there's always more than one approach and to choose the one you feel most comfortable with.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.