Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jeancollas/mauiblazordependencyinjection

Resolver allowing to share dependency injection between Maui and Blazor
https://github.com/jeancollas/mauiblazordependencyinjection

blazor blazor-webassembly dependency-injection maui maui-blazor

Last synced: 3 days ago
JSON representation

Resolver allowing to share dependency injection between Maui and Blazor

Awesome Lists containing this project

README

        

## Presentation
This tool aims at simplifying the share of dependency injection between Maui and Blazor in a mixed environment

## Nuget
[DependencyInjectionMauiBlazor](https://www.nuget.org/packages/DependencyInjectionMauiBlazor/)
[![NuGet Status](https://img.shields.io/nuget/v/DependencyInjectionMauiBlazor.svg?style=flat)](https://www.nuget.org/packages/DependencyInjectionMauiBlazor/)

## Sample

![MAUI Blazor sample](maui-blazor-sample.gif "MAUI Blazor sharing context sample")

## How to use ?

**In MauiProgram class**
```
using MauiBlazorDependencyInjection;

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

builder.Services.AddBlazorWebView();
builder.Services.AddSingleton();

// Register any other service / ViewModel / Page here
builder.Services.AddSingleton();

// Pre-build the app
var app = builder.Build();
// Intercept and register the ServiceProvider
app.Services.UseResolver();
// Return the app as usual
return app;
}
}
```

**a. Use it as your factory from Maui side**

``` csharp
Resolver.ServiceProvider.GetRequiredService();
Console.WriteLine($"Instance number {TestSingletonService.Index}");
```
**b. The services instances will be shared with your Blazor `@inject` services.**
``` razor
@inject TestSingletonService tester
Instance number @(tester.Index).
```