diff --git a/Bootstrappers/Bootstrappers.csproj b/Bootstrappers/Bootstrappers.csproj index 1ccd9ff..f1f0ebb 100644 --- a/Bootstrappers/Bootstrappers.csproj +++ b/Bootstrappers/Bootstrappers.csproj @@ -9,8 +9,9 @@ Properties Bootstrappers Bootstrappers - v4.5 + v4.7.2 512 + true @@ -41,6 +42,15 @@ packages\Castle.Windsor.3.3.0\lib\net45\Castle.Windsor.dll + + packages\Microsoft.Bcl.AsyncInterfaces.5.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll + + + packages\Microsoft.Extensions.DependencyInjection.5.0.1\lib\net461\Microsoft.Extensions.DependencyInjection.dll + + + packages\Microsoft.Extensions.DependencyInjection.Abstractions.5.0.0\lib\net461\Microsoft.Extensions.DependencyInjection.Abstractions.dll + packages\Unity.3.5.1404.0\lib\net45\Microsoft.Practices.Unity.dll @@ -68,6 +78,12 @@ + + packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + @@ -80,12 +96,14 @@ + + diff --git a/Bootstrappers/CastleWindsorBootstrapper.cs b/Bootstrappers/CastleWindsorBootstrapper.cs index fe2a926..b571d2a 100644 --- a/Bootstrappers/CastleWindsorBootstrapper.cs +++ b/Bootstrappers/CastleWindsorBootstrapper.cs @@ -39,7 +39,7 @@ namespace Bootstrappers }; // Stylet does its own disposal of ViewModels: Castle Windsor shouldn't be doing the same - // Castle Windsor seems to be ver opinionated on this point, insisting that the container + // Castle Windsor seems to be very opinionated on this point, insisting that the container // should be responsible for disposing all components. This is at odds with Stylet's approach // (and indeed common sense). #pragma warning disable CS0618 // Type or member is obsolete diff --git a/Bootstrappers/MicrosoftDependencyInjectionBootstrapper.cs b/Bootstrappers/MicrosoftDependencyInjectionBootstrapper.cs new file mode 100644 index 0000000..1fa3e46 --- /dev/null +++ b/Bootstrappers/MicrosoftDependencyInjectionBootstrapper.cs @@ -0,0 +1,79 @@ +using Autofac; +using Microsoft.Extensions.DependencyInjection; +using Stylet; +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace Bootstrappers +{ + public class MicrosoftDependencyInjectionBootstrapper : BootstrapperBase where TRootViewModel : class + { + private ServiceProvider serviceProvider; + + private object _rootViewModel; + protected virtual object RootViewModel + { + get { return this._rootViewModel ?? (this._rootViewModel = this.GetInstance(typeof(TRootViewModel))); } + } + + public IServiceProvider ServiceProvider + { + get { return this.serviceProvider; } + } + + protected override void ConfigureBootstrapper() + { + var services = new ServiceCollection(); + this.DefaultConfigureIoC(services); + this.ConfigureIoC(services); + this.serviceProvider = services.BuildServiceProvider(); + } + + /// + /// Carries out default configuration of the IoC container. Override if you don't want to do this + /// + protected virtual void DefaultConfigureIoC(IServiceCollection services) + { + var viewManagerConfig = new ViewManagerConfig() + { + ViewFactory = this.GetInstance, + ViewAssemblies = new List() { this.GetType().Assembly } + }; + + services.AddSingleton(new ViewManager(viewManagerConfig)); + services.AddTransient(); + + services.AddSingleton(this); + services.AddSingleton(); + services.AddSingleton(); + services.AddTransient(); // Not singleton! + // Also need a factory + services.AddSingleton>(() => new MessageBoxViewModel()); + } + + /// + /// Override to add your own types to the IoC container. + /// + protected virtual void ConfigureIoC(IServiceCollection services) { } + + public override object GetInstance(Type type) + { + return this.serviceProvider.GetRequiredService(type); + } + + protected override void Launch() + { + base.DisplayRootView(this.RootViewModel); + } + + public override void Dispose() + { + base.Dispose(); + + ScreenExtensions.TryDispose(this._rootViewModel); + if (this.serviceProvider != null) + this.serviceProvider.Dispose(); + } + } +} diff --git a/Bootstrappers/Tests/BootstrapperTests.cs b/Bootstrappers/Tests/BootstrapperTests.cs index 0610d2f..8707f49 100644 --- a/Bootstrappers/Tests/BootstrapperTests.cs +++ b/Bootstrappers/Tests/BootstrapperTests.cs @@ -159,6 +159,9 @@ namespace Bootstrappers.Tests [Test] public void DoesNotDisposeTransientInstances() { + if (!this.Autobinds) + Assert.Ignore("Autobinding not supported"); + StubType.Reset(); var vm = this.bootstrapper.GetInstance(typeof(StubType)); diff --git a/Bootstrappers/Tests/MicrosoftDependencyInjectionTests.cs b/Bootstrappers/Tests/MicrosoftDependencyInjectionTests.cs new file mode 100644 index 0000000..0581131 --- /dev/null +++ b/Bootstrappers/Tests/MicrosoftDependencyInjectionTests.cs @@ -0,0 +1,71 @@ +using Autofac; +using Microsoft.Extensions.DependencyInjection; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bootstrappers.Tests +{ + public class MyMicrosoftDependencyInjectionBootstrapper : MicrosoftDependencyInjectionBootstrapper, ITestBootstrapper + { + public List ConfigureLog { get; set; } + + public int DisposeCount { get; private set; } + + public MyMicrosoftDependencyInjectionBootstrapper() + { + this.ConfigureLog = new List(); + } + + protected override void Configure() + { + base.Configure(); + this.ConfigureLog.Add("Configure"); + } + + protected override void DefaultConfigureIoC(IServiceCollection services) + { + base.DefaultConfigureIoC(services); + this.ConfigureLog.Add("DefaultConfigureIoC"); + } + + protected override void ConfigureIoC(IServiceCollection services) + { + base.ConfigureIoC(services); + this.ConfigureLog.Add("ConfigureIoC"); + } + + public new object GetInstance(Type type) + { + return base.GetInstance(type); + } + + public new void ConfigureBootstrapper() + { + base.ConfigureBootstrapper(); + } + + public override void Dispose() + { + base.Dispose(); + this.DisposeCount++; + } + } + + [TestFixture(Category = "ServiceCollection")] + public class MicrosoftDependencyInjectionTests : BootstrapperTests + { + public MicrosoftDependencyInjectionTests() + { + this.Autobinds = false; + } + + public override MyMicrosoftDependencyInjectionBootstrapper CreateBootstrapper() + { + return new MyMicrosoftDependencyInjectionBootstrapper(); + } + } +} diff --git a/Bootstrappers/packages.config b/Bootstrappers/packages.config index 8b5cd13..d13f404 100644 --- a/Bootstrappers/packages.config +++ b/Bootstrappers/packages.config @@ -3,9 +3,14 @@ + + + + + \ No newline at end of file