Skip to content

Commit

Permalink
Add preliminary MvxHost and MvxHostBuilder inspired by Shiny and MAUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheesebaron committed May 30, 2023
1 parent 662e331 commit de08dcb
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PackageVersion Include="AsyncFixer" Version="1.6.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PakcageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PakcageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
Expand Down
2 changes: 1 addition & 1 deletion MvvmCross-macos.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"MvvmCross.Plugins\\All\\MvvmCross.Plugin.All.csproj",
"MvvmCross.Plugins\\Color\\MvvmCross.Plugin.Color.csproj",
"MvvmCross.Plugins\\FieldBinding\\MvvmCross.Plugin.FieldBinding.csproj",
"MvvmCross.Plugins\\Json\\MvvmCross.Plugin.Json.csproj",
"MvvmCross.Plugins\\JsonLocalization\\MvvmCross.Plugin.JsonLocalization.csproj",
"MvvmCross.Plugins\\Json\\MvvmCross.Plugin.Json.csproj",
"MvvmCross.Plugins\\Messenger\\MvvmCross.Plugin.Messenger.csproj",
"MvvmCross.Plugins\\MethodBinding\\MvvmCross.Plugin.MethodBinding.csproj",
"MvvmCross.Plugins\\ResourceLoader\\MvvmCross.Plugin.ResourceLoader.csproj",
Expand Down
12 changes: 12 additions & 0 deletions MvvmCross/Hosting/IMvxHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#nullable enable
using Microsoft.Extensions.Logging;

namespace MvvmCross.Hosting;

public interface IMvxHost : IDisposable
{
IServiceProvider Services { get; }
ILoggerFactory Logging { get; }

void Run();
}
12 changes: 12 additions & 0 deletions MvvmCross/Hosting/IMvxHostBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#nullable enable
using Microsoft.Extensions.DependencyInjection;

namespace MvvmCross.Hosting;

public interface IMvxHostBuilder
{
Func<IServiceCollection, IServiceProvider>? ConfigureContainer { get; }
IServiceCollection Services { get; }

IMvxHost Build();
}
48 changes: 48 additions & 0 deletions MvvmCross/Hosting/MvxHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#nullable enable
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MvvmCross.Base;

namespace MvvmCross.Hosting;

public sealed class MvxHost : IMvxHost
{
private static IMvxHost? _currentHost;

public static IMvxHost Current
{
get
{
if (_currentHost == null)
throw new InvalidOperationException("MvxHost has not been created yet. This means MvvmCross hasn't been initialized properly.");

return _currentHost;
}
private set => _currentHost = value;
}

public static bool IsInitialized => _currentHost != null;
public static IServiceProvider ServiceProvider => Current.Services;
public static ILoggerFactory LoggingFactory => Current.Logging;
public static T? GetService<T>() => ServiceProvider.GetService<T>();

public IServiceProvider Services { get; init; }
public ILoggerFactory Logging { get; init; }

public MvxHost(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
{
Services = serviceProvider;
Logging = loggerFactory;
}

public void Run()
{
Current = this;
}

public void Dispose()
{
Services.DisposeIfDisposable();
Logging.Dispose();
}
}
42 changes: 42 additions & 0 deletions MvvmCross/Hosting/MvxHostBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#nullable enable
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace MvvmCross.Hosting;

public class MvxHostBuilder : IMvxHostBuilder
{
public Func<IServiceCollection, IServiceProvider> ConfigureContainer { get; }
public IServiceCollection Services { get; }

public MvxHostBuilder(
IServiceCollection? serviceCollection = null,
Func<IServiceCollection, IServiceProvider>? configureContainer = null)
{
Services = serviceCollection ?? new ServiceCollection();
ConfigureContainer = configureContainer ?? (s => s.BuildServiceProvider());
}

public virtual IMvxHost Build()
{
ConfigureDefaultNullLogging(Services);
var serviceCollection = ConfigureContainer(Services);
var loggerFactory = serviceCollection.GetRequiredService<ILoggerFactory>();

var host = new MvxHost(serviceCollection, loggerFactory);

#if NET7_0_OR_GREATER
#endif

host.Run();

return host;
}

private static void ConfigureDefaultNullLogging(IServiceCollection services)
{
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
}
}
2 changes: 2 additions & 0 deletions MvvmCross/MvvmCross.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ This package contains the 'Core' libraries for MvvmCross</Description>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion Projects/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
<Version>7.0.0</Version>

<IsLibraryProject>$(MSBuildProjectName.Contains('MvvmCross'))</IsLibraryProject>
<IsXamarinForms>$(MSBuildProjectName.Contains('.Forms'))</IsXamarinForms>
</PropertyGroup>
</Project>

0 comments on commit de08dcb

Please sign in to comment.