From 4b6d8cb5752a2374c4d95e3818f325dec27bdf43 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Thu, 25 Jun 2020 10:46:53 -0700 Subject: [PATCH 01/13] update DryIoc for Prism 8 --- .gitmodules | 3 + Directory.build.props | 2 +- external/Prism | 1 + .../ContainerLocator.cs | 62 ---- .../ContainerResolutionException.cs | 32 -- .../IExtendedContainerRegistry.cs | 39 --- .../IExtendedContainerRegistryExtensions.cs | 234 ------------- .../IScopeProvider.cs | 27 -- .../IScopedFactoryRegistry.cs | 19 -- .../IScopedFactoryRegistryExtensions.cs | 56 ---- .../IServiceProviderExtensions.cs | 12 +- .../PreserveAttribute.cs | 49 --- .../Prism.Container.Extensions.csproj | 3 +- .../PrismServiceScope.cs | 27 ++ .../ContainerExtension.cs | 30 ++ .../PreserveAttribute.cs | 47 --- .../Prism.DryIoc.Extensions.csproj | 8 +- .../PrismContainerExtension.cs | 308 +----------------- .../Properties/AssemblyInfo.cs | 5 + .../Prism.Forms.Extended.csproj | 2 +- 20 files changed, 99 insertions(+), 867 deletions(-) create mode 100644 .gitmodules create mode 160000 external/Prism delete mode 100644 src/Prism.Container.Extensions/ContainerLocator.cs delete mode 100644 src/Prism.Container.Extensions/ContainerResolutionException.cs delete mode 100644 src/Prism.Container.Extensions/IExtendedContainerRegistry.cs delete mode 100644 src/Prism.Container.Extensions/IExtendedContainerRegistryExtensions.cs delete mode 100644 src/Prism.Container.Extensions/IScopeProvider.cs delete mode 100644 src/Prism.Container.Extensions/IScopedFactoryRegistry.cs delete mode 100644 src/Prism.Container.Extensions/IScopedFactoryRegistryExtensions.cs delete mode 100644 src/Prism.Container.Extensions/PreserveAttribute.cs create mode 100644 src/Prism.Container.Extensions/PrismServiceScope.cs create mode 100644 src/Prism.DryIoc.Extensions/ContainerExtension.cs delete mode 100644 src/Prism.DryIoc.Extensions/PreserveAttribute.cs create mode 100644 src/Prism.DryIoc.Extensions/Properties/AssemblyInfo.cs diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1c7e06c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/Prism"] + path = external/Prism + url = git@github.com:PrismLibrary/Prism.git diff --git a/Directory.build.props b/Directory.build.props index 653fd7c..f832fd5 100644 --- a/Directory.build.props +++ b/Directory.build.props @@ -4,7 +4,7 @@ dansiegel © Dan Siegel. All rights reserved. - 7.2.0 + 8.0 en-US true snupkg diff --git a/external/Prism b/external/Prism new file mode 160000 index 0000000..03cdfaa --- /dev/null +++ b/external/Prism @@ -0,0 +1 @@ +Subproject commit 03cdfaa7744a04944581aed95c00704955ec623a diff --git a/src/Prism.Container.Extensions/ContainerLocator.cs b/src/Prism.Container.Extensions/ContainerLocator.cs deleted file mode 100644 index 2f38e8d..0000000 --- a/src/Prism.Container.Extensions/ContainerLocator.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.IO; -using System.Linq; -using System.Reflection; -using Prism.Ioc; - -namespace Prism.Container.Extensions -{ -#pragma warning disable CS1591 - [EditorBrowsable(EditorBrowsableState.Never)] - public static class ContainerLocator - { - private static ContainerExtensionAttribute _locatedExtension; - - [EditorBrowsable(EditorBrowsableState.Never)] - public static IContainerExtension Locate() - { - if (_locatedExtension != null) - { - return _locatedExtension.Container; - } - else if (!LocatedAssembly(AppDomain.CurrentDomain.GetAssemblies(), out _locatedExtension)) - { - throw new DllNotFoundException("No assembly containing a reference to an IContainerExtension implementation could be found. You must have a reference to one of the DI Container Extensions in your final project"); - } - - return _locatedExtension.Container; - } - - [EditorBrowsable(EditorBrowsableState.Never)] - public static void LocatePreservedReference(Type type) - { - if (_locatedExtension is null) - { - if (!LocatedAssembly(new[] { type.Assembly }, out _locatedExtension)) - { - throw new DllNotFoundException("No assembly containing a reference to an IContainerExtension implementation could be found. You must have a reference to one of the DI Container Extensions in your final project"); - } - - Console.WriteLine($"Located Container: {_locatedExtension.Container.GetType().FullName}"); - } - } - - private static bool LocatedAssembly(IEnumerable assemblies, out ContainerExtensionAttribute attribute) - { - attribute = null; - foreach(var assembly in assemblies) - { - attribute = assembly.GetCustomAttribute(); - if(attribute != null) - { - return true; - } - } - - return false; - } - } -#pragma warning restore CS1591 -} diff --git a/src/Prism.Container.Extensions/ContainerResolutionException.cs b/src/Prism.Container.Extensions/ContainerResolutionException.cs deleted file mode 100644 index 46cd0e3..0000000 --- a/src/Prism.Container.Extensions/ContainerResolutionException.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; - -namespace Prism.Ioc -{ - public class ContainerResolutionException : Exception - { - public ContainerResolutionException(Type serviceType, Exception innerException) - : this(serviceType, null, innerException) - { - } - - public ContainerResolutionException(Type serviceType, string serviceName, Exception innerException) - : base(GetErrorMessage(serviceType, serviceName), innerException) - { - ServiceType = serviceType; - ServiceName = serviceName; - } - - public Type ServiceType { get; } - - public string ServiceName { get; } - - private static string GetErrorMessage(Type type, string name) - { - var message = $"An unexpected error occurred while resolving '{type.FullName}'"; - if (!string.IsNullOrEmpty(name)) - message += $", with the service name '{name}'"; - - return message; - } - } -} diff --git a/src/Prism.Container.Extensions/IExtendedContainerRegistry.cs b/src/Prism.Container.Extensions/IExtendedContainerRegistry.cs deleted file mode 100644 index 3a106d2..0000000 --- a/src/Prism.Container.Extensions/IExtendedContainerRegistry.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.ComponentModel; - -namespace Prism.Ioc -{ - [EditorBrowsable(EditorBrowsableState.Never)] - public interface IExtendedContainerRegistry : IContainerRegistry, IServiceProvider - { - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterMany(Type implementingType, params Type[] serviceTypes); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterManySingleton(Type implementingType, params Type[] serviceTypes); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterDelegate(Type serviceType, Func factoryMethod); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterDelegate(Type serviceType, Func factoryMethod); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterDelegate(Type serviceType, Func factoryMethod); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterSingletonFromDelegate(Type serviceType, Func factoryMethod); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterSingletonFromDelegate(Type serviceType, Func factoryMethod); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterSingletonFromDelegate(Type serviceType, Func factoryMethod); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterScoped(Type serviceType); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterScoped(Type serviceType, Type implementationType); - } -} diff --git a/src/Prism.Container.Extensions/IExtendedContainerRegistryExtensions.cs b/src/Prism.Container.Extensions/IExtendedContainerRegistryExtensions.cs deleted file mode 100644 index 01d3c4e..0000000 --- a/src/Prism.Container.Extensions/IExtendedContainerRegistryExtensions.cs +++ /dev/null @@ -1,234 +0,0 @@ -using System; -using System.Linq; - -namespace Prism.Ioc -{ - public static class IExtendedContainerRegistryExtensions - { - #region Register Many - - public static IContainerRegistry RegisterManySingleton(this IContainerRegistry containerRegistry, Type implementingType, params Type[] serviceTypes) - { - if(containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterManySingleton(implementingType, serviceTypes); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterManySingleton(this IContainerRegistry containerRegistry, params Type[] serviceTypes) => - containerRegistry.RegisterManySingleton(typeof(T), serviceTypes); - - public static IContainerRegistry RegisterMany(this IContainerRegistry containerRegistry, Type implementingType, params Type[] serviceTypes) - { - if (containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterMany(implementingType, serviceTypes); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterMany(this IContainerRegistry containerRegistry, params Type[] serviceTypes) => - containerRegistry.RegisterMany(typeof(T), serviceTypes); - - #endregion Register Many - - public static IContainerRegistry RegisterDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - containerRegistry.RegisterDelegate(typeof(T), () => factoryMethod()); - - public static IContainerRegistry RegisterDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterDelegate(serviceType, factoryMethod); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - containerRegistry.RegisterDelegate(typeof(T), factoryMethod); - - public static IContainerRegistry RegisterDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterDelegate(serviceType, factoryMethod); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - containerRegistry.RegisterDelegate(typeof(T), factoryMethod); - - public static IContainerRegistry RegisterDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterDelegate(serviceType, factoryMethod); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterSingletonFromDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - containerRegistry.RegisterSingletonFromDelegate(typeof(T), factoryMethod); - - public static IContainerRegistry RegisterSingletonFromDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterSingletonFromDelegate(serviceType, factoryMethod); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterSingletonFromDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - containerRegistry.RegisterSingletonFromDelegate(typeof(T), factoryMethod); - - public static IContainerRegistry RegisterSingletonFromDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterSingletonFromDelegate(serviceType, factoryMethod); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterSingletonFromDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - containerRegistry.RegisterSingletonFromDelegate(typeof(T), factoryMethod); - - public static IContainerRegistry RegisterSingletonFromDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterSingletonFromDelegate(serviceType, factoryMethod); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterScoped(this IContainerRegistry containerRegistry) => - containerRegistry.RegisterScoped(typeof(T)); - - public static IContainerRegistry RegisterScoped(this IContainerRegistry containerRegistry, Type serviceType) => - containerRegistry.RegisterScoped(serviceType, serviceType); - - public static IContainerRegistry RegisterScoped(IContainerRegistry containerRegistry) => - containerRegistry.RegisterScoped(typeof(TService), typeof(TImplementation)); - - public static IContainerRegistry RegisterScoped(this IContainerRegistry containerRegistry, Type serviceType, Type implementationType) - { - if (containerRegistry is IExtendedContainerRegistry ecr) - { - ecr.RegisterScoped(serviceType, implementationType); - return ecr; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterSingletonOnce(this IContainerRegistry containerRegistry) - where TImp : T - { - if (!containerRegistry.IsRegistered()) - { - containerRegistry.RegisterSingleton(); - } - return containerRegistry; - } - - public static IContainerRegistry RegisterSingletonOnce(this IContainerRegistry containerRegistry, string name) - where TImp : T - { - if (!containerRegistry.IsRegistered(name)) - { - containerRegistry.RegisterSingleton(name); - } - return containerRegistry; - } - - public static IContainerRegistry RegisterOnce(this IContainerRegistry containerRegistry) - where TImp : T - { - if (!containerRegistry.IsRegistered()) - { - containerRegistry.Register(); - } - return containerRegistry; - } - - public static IContainerRegistry RegisterOnce(this IContainerRegistry containerRegistry, string name) - where TImp : T - { - if (!containerRegistry.IsRegistered(name)) - { - containerRegistry.Register(name); - } - return containerRegistry; - } - - public static IContainerRegistry RegisterManyOnce(this IContainerRegistry containerRegistry, params Type[] services) - { - if(!services?.Any() ?? false) - { - services = typeof(T).GetInterfaces(); - } - - services = services.Where(x => !containerRegistry.IsRegistered(x)).ToArray(); - - if (services?.Any() ?? false) - { - containerRegistry.RegisterMany(services); - } - return containerRegistry; - } - - public static IContainerRegistry RegisterManySingletonOnce(this IContainerRegistry containerRegistry, params Type[] services) - { - if (!services?.Any() ?? false) - { - services = typeof(T).GetInterfaces(); - } - - services = services.Where(x => !containerRegistry.IsRegistered(x)).ToArray(); - - if (services?.Any() ?? false) - { - containerRegistry.RegisterManySingleton(services); - } - return containerRegistry; - } - } -} diff --git a/src/Prism.Container.Extensions/IScopeProvider.cs b/src/Prism.Container.Extensions/IScopeProvider.cs deleted file mode 100644 index cdcd28f..0000000 --- a/src/Prism.Container.Extensions/IScopeProvider.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.ComponentModel; - -namespace Prism.Ioc -{ - [EditorBrowsable(EditorBrowsableState.Never)] - public interface IScopeProvider - { - [EditorBrowsable(EditorBrowsableState.Never)] - void CreateScope(); - } - - public static class IContainerProviderExtensions - { - public static void CreateScope(this IContainerProvider container) - { - if(container is IScopeProvider scopeProvider) - { - scopeProvider.CreateScope(); - } - else - { - throw new NotSupportedException("The specified container provider does not implement IScopeProvider"); - } - } - } -} diff --git a/src/Prism.Container.Extensions/IScopedFactoryRegistry.cs b/src/Prism.Container.Extensions/IScopedFactoryRegistry.cs deleted file mode 100644 index c19b6bd..0000000 --- a/src/Prism.Container.Extensions/IScopedFactoryRegistry.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.ComponentModel; -using Prism.Ioc; - -namespace Prism.Container.Extensions -{ - [EditorBrowsable(EditorBrowsableState.Never)] - public interface IScopedFactoryRegistry - { - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterScopedFromDelegate(Type serviceType, Func factoryMethod); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterScopedFromDelegate(Type serviceType, Func factoryMethod); - - [EditorBrowsable(EditorBrowsableState.Never)] - IContainerRegistry RegisterScopedFromDelegate(Type serviceType, Func factoryMethod); - } -} diff --git a/src/Prism.Container.Extensions/IScopedFactoryRegistryExtensions.cs b/src/Prism.Container.Extensions/IScopedFactoryRegistryExtensions.cs deleted file mode 100644 index b3a7119..0000000 --- a/src/Prism.Container.Extensions/IScopedFactoryRegistryExtensions.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using Prism.Container.Extensions; - -namespace Prism.Ioc -{ - public static class IScopedFactoryRegistryExtensions - { - public static IContainerRegistry RegisterScopedFromDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - RegisterScopedFromDelegate(containerRegistry, typeof(T), factoryMethod); - - public static IContainerRegistry RegisterScopedFromDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IScopedFactoryRegistry scopedRegistry) - { - scopedRegistry.RegisterScopedFromDelegate(serviceType, factoryMethod); - return containerRegistry; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterScopedFromDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - RegisterScopedFromDelegate(containerRegistry, typeof(T), factoryMethod); - - public static IContainerRegistry RegisterScopedFromDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IScopedFactoryRegistry scopedRegistry) - { - scopedRegistry.RegisterScopedFromDelegate(serviceType, factoryMethod); - return containerRegistry; - } - else - { - throw new NotImplementedException(); - } - } - - public static IContainerRegistry RegisterScopedFromDelegate(this IContainerRegistry containerRegistry, Func factoryMethod) => - RegisterScopedFromDelegate(containerRegistry, typeof(T), factoryMethod); - - public static IContainerRegistry RegisterScopedFromDelegate(this IContainerRegistry containerRegistry, Type serviceType, Func factoryMethod) - { - if (containerRegistry is IScopedFactoryRegistry scopedRegistry) - { - scopedRegistry.RegisterScopedFromDelegate(serviceType, factoryMethod); - return containerRegistry; - } - else - { - throw new NotImplementedException(); - } - } - } -} diff --git a/src/Prism.Container.Extensions/IServiceProviderExtensions.cs b/src/Prism.Container.Extensions/IServiceProviderExtensions.cs index 7a82392..ea3f873 100644 --- a/src/Prism.Container.Extensions/IServiceProviderExtensions.cs +++ b/src/Prism.Container.Extensions/IServiceProviderExtensions.cs @@ -37,20 +37,20 @@ internal static void RegisterTypesWithPrismContainer(IContainerRegistry containe else if (service.ImplementationInstance != null) containerRegistry.RegisterInstance(service.ServiceType, service.ImplementationInstance); else if (service.ImplementationFactory != null) - containerRegistry.RegisterSingletonFromDelegate(service.ServiceType, service.ImplementationFactory); + containerRegistry.RegisterSingleton(service.ServiceType, c => ServiceFactory(c, service.ImplementationFactory)); break; case ServiceLifetime.Transient: if (service.ImplementationType != null) containerRegistry.Register(service.ServiceType, service.ImplementationType); else if (service.ImplementationFactory != null) - containerRegistry.RegisterDelegate(service.ServiceType, service.ImplementationFactory); + containerRegistry.Register(service.ServiceType, c => ServiceFactory(c, service.ImplementationFactory)); // Transient Lifetime cannot occur with an Instance break; case ServiceLifetime.Scoped: if (service.ImplementationType != null) containerRegistry.RegisterScoped(service.ServiceType, service.ImplementationType); else if (service.ImplementationType is null) - containerRegistry.RegisterScopedFromDelegate(service.ServiceType, service.ImplementationFactory); + containerRegistry.RegisterScoped(service.ServiceType, c => ServiceFactory(c, service.ImplementationFactory)); else if (service.ServiceType.IsAbstract) throw new NotSupportedException($"Cannot register the service {service.ServiceType.FullName} as it is an abstract type"); else if (service.ServiceType.IsInterface) @@ -61,5 +61,11 @@ internal static void RegisterTypesWithPrismContainer(IContainerRegistry containe } } } + + private static object ServiceFactory(IContainerProvider container, Func implementationFactory) + { + var sp = container.Resolve(); + return implementationFactory(sp); + } } } diff --git a/src/Prism.Container.Extensions/PreserveAttribute.cs b/src/Prism.Container.Extensions/PreserveAttribute.cs deleted file mode 100644 index 455cdfd..0000000 --- a/src/Prism.Container.Extensions/PreserveAttribute.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.ComponentModel; -using Prism.Container.Extensions; -using Prism.Ioc; - -[assembly: Preserve] - -namespace Prism.Container.Extensions -{ - [AttributeUsage( - AttributeTargets.Assembly - | AttributeTargets.Class - | AttributeTargets.Struct - | AttributeTargets.Enum - | AttributeTargets.Constructor - | AttributeTargets.Method - | AttributeTargets.Property - | AttributeTargets.Field - | AttributeTargets.Event - | AttributeTargets.Interface - | AttributeTargets.Delegate, - AllowMultiple = true)] - [EditorBrowsable(EditorBrowsableState.Never)] - public sealed class PreserveAttribute : Attribute - { - public bool AllMembers { get; } - public bool Conditional { get; } - - public PreserveAttribute(bool allMembers, bool conditional) - { - AllMembers = allMembers; - Conditional = conditional; - } - - public PreserveAttribute() - : this(true, false) - { - } - - public PreserveAttribute(Type referenceType) - : this(true, false) - { - if(typeof(IContainerExtension).IsAssignableFrom(referenceType)) - { - ContainerLocator.LocatePreservedReference(referenceType); - } - } - } -} diff --git a/src/Prism.Container.Extensions/Prism.Container.Extensions.csproj b/src/Prism.Container.Extensions/Prism.Container.Extensions.csproj index 9e89980..8ba07b5 100644 --- a/src/Prism.Container.Extensions/Prism.Container.Extensions.csproj +++ b/src/Prism.Container.Extensions/Prism.Container.Extensions.csproj @@ -4,12 +4,11 @@ netstandard2.0;net461;net47 Prism Container Extension Abstractions prism di extensions - - + diff --git a/src/Prism.Container.Extensions/PrismServiceScope.cs b/src/Prism.Container.Extensions/PrismServiceScope.cs new file mode 100644 index 0000000..decef9f --- /dev/null +++ b/src/Prism.Container.Extensions/PrismServiceScope.cs @@ -0,0 +1,27 @@ +using System; +using Microsoft.Extensions.DependencyInjection; + +namespace Prism.Ioc +{ + public class PrismServiceScope : IServiceScope + { + private IScopedProvider _scopedProvider; + + public PrismServiceScope(IScopedProvider scopedProvider) + { + _scopedProvider = scopedProvider; + ServiceProvider = new PrismServiceProvider(scopedProvider.CurrentScope); + } + + public IServiceProvider ServiceProvider { get; } + + public void Dispose() + { + if (_scopedProvider != null) + { + _scopedProvider.Dispose(); + _scopedProvider = null; + } + } + } +} diff --git a/src/Prism.DryIoc.Extensions/ContainerExtension.cs b/src/Prism.DryIoc.Extensions/ContainerExtension.cs new file mode 100644 index 0000000..7e5321a --- /dev/null +++ b/src/Prism.DryIoc.Extensions/ContainerExtension.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DryIoc; +using Microsoft.Extensions.DependencyInjection; +using Prism.DryIoc; +using Prism.Ioc; + +namespace Prism.DryIoc +{ + partial class DryIocContainerExtension : IServiceScopeFactory + { + public DryIocContainerExtension(IContainer container) + { + Instance = container; + Instance.RegisterInstanceMany(new[] + { + typeof(IContainerExtension), + typeof(IContainerProvider), + typeof(IServiceScopeFactory) + }, this); + } + + IServiceScope IServiceScopeFactory.CreateScope() + { + var scope = CreateScopeInternal(); + return new PrismServiceScope(scope); + } + } +} diff --git a/src/Prism.DryIoc.Extensions/PreserveAttribute.cs b/src/Prism.DryIoc.Extensions/PreserveAttribute.cs deleted file mode 100644 index a29b25a..0000000 --- a/src/Prism.DryIoc.Extensions/PreserveAttribute.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.ComponentModel; -using Prism.Container.Extensions; -using Prism.Ioc; - -namespace Prism.DryIoc -{ - [AttributeUsage( - AttributeTargets.Assembly - | AttributeTargets.Class - | AttributeTargets.Struct - | AttributeTargets.Enum - | AttributeTargets.Constructor - | AttributeTargets.Method - | AttributeTargets.Property - | AttributeTargets.Field - | AttributeTargets.Event - | AttributeTargets.Interface - | AttributeTargets.Delegate, - AllowMultiple = true)] - [EditorBrowsable(EditorBrowsableState.Never)] - public sealed class PreserveAttribute : Attribute - { - public bool AllMembers { get; } - public bool Conditional { get; } - - public PreserveAttribute(bool allMembers, bool conditional) - { - AllMembers = allMembers; - Conditional = conditional; - } - - public PreserveAttribute() - : this(true, false) - { - } - - public PreserveAttribute(Type referenceType) - : this(true, false) - { - if (typeof(IContainerExtension).IsAssignableFrom(referenceType)) - { - ContainerLocator.LocatePreservedReference(referenceType); - } - } - } -} diff --git a/src/Prism.DryIoc.Extensions/Prism.DryIoc.Extensions.csproj b/src/Prism.DryIoc.Extensions/Prism.DryIoc.Extensions.csproj index 712a807..a3769e9 100644 --- a/src/Prism.DryIoc.Extensions/Prism.DryIoc.Extensions.csproj +++ b/src/Prism.DryIoc.Extensions/Prism.DryIoc.Extensions.csproj @@ -3,6 +3,7 @@ netstandard2.0;Xamarin.iOS10 Prism Container Extensions for DryIoc + $(DefineConstants);ContainerExtensions @@ -11,7 +12,12 @@ - + + + + + + diff --git a/src/Prism.DryIoc.Extensions/PrismContainerExtension.cs b/src/Prism.DryIoc.Extensions/PrismContainerExtension.cs index 6f683ee..8d068e6 100644 --- a/src/Prism.DryIoc.Extensions/PrismContainerExtension.cs +++ b/src/Prism.DryIoc.Extensions/PrismContainerExtension.cs @@ -1,311 +1,31 @@ -using System; -using System.ComponentModel; -using System.Linq; -using System.Runtime.CompilerServices; +using System; using DryIoc; -using Microsoft.Extensions.DependencyInjection; -using Prism.Container.Extensions; -using Prism.DryIoc; using Prism.Ioc; -using IContainer = DryIoc.IContainer; -[assembly: Prism.DryIoc.Preserve] -[assembly: ContainerExtension(typeof(PrismContainerExtension))] -[assembly: InternalsVisibleTo("Prism.DryIoc.Extensions.Tests")] -[assembly: InternalsVisibleTo("Prism.DryIoc.Forms.Extended.Tests")] -[assembly: InternalsVisibleTo("Shiny.Prism.Tests")] namespace Prism.DryIoc { - public sealed partial class PrismContainerExtension : IContainerExtension, IExtendedContainerRegistry, IScopeProvider, IScopedFactoryRegistry, IServiceScopeFactory + public static class PrismContainerExtension { - private static IContainerExtension _current; - public static IContainerExtension Current - { - get - { - if(_current is null) - { - Create(); - } - - return _current; - } - } - - internal static void Reset() - { - _current = null; - GC.Collect(Int32.MaxValue, GCCollectionMode.Forced); - GC.WaitForFullGCComplete(); - } - - public static IContainerExtension Create() => - Create(CreateContainerRules()); - - public static IContainerExtension Create(Rules rules) => - Create(new global::DryIoc.Container(rules)); - - public static IContainerExtension Create(IContainer container) - { - if(_current != null) - { - throw new NotSupportedException($"An instance of {nameof(PrismContainerExtension)} has already been created."); - } - - return new PrismContainerExtension(container); - } - - [EditorBrowsable(EditorBrowsableState.Never)] - public static Rules CreateContainerRules() => Rules.Default.WithConcreteTypeDynamicRegistrations() - .With(Made.Of(FactoryMethod.ConstructorWithResolvableArguments)) - .WithFuncAndLazyWithoutRegistration() - .WithTrackingDisposableTransients() - .WithoutFastExpressionCompiler() - .WithDefaultIfAlreadyRegistered(IfAlreadyRegistered.AppendNewImplementation) - .WithFactorySelector(Rules.SelectLastRegisteredFactory()); - - private PrismContainerExtension() - : this(CreateContainerRules()) - { - } - - private PrismContainerExtension(Rules rules) - : this(new global::DryIoc.Container(rules)) - { - } - - private PrismContainerExtension(IContainer container) - { - _current = this; - Instance = container; - Instance.RegisterInstanceMany(new[] - { - typeof(IContainerExtension), - typeof(IContainerRegistry), - typeof(IContainerProvider), - typeof(IServiceProvider), - typeof(IServiceScopeFactory) - }, this); - } - - private ServiceScope _currentScope; - - public IContainer Instance { get; } - - public void FinalizeExtension() { } - - public IContainerRegistry RegisterInstance(Type type, object instance) - { - Instance.RegisterInstance(type, instance); - return this; - } - - public IContainerRegistry RegisterInstance(Type type, object instance, string name) - { - Instance.RegisterInstance(type, instance, ifAlreadyRegistered: IfAlreadyRegistered.Replace, serviceKey: name); - return this; - } - - public IContainerRegistry RegisterSingleton(Type from, Type to) - { - Instance.Register(from, to, Reuse.Singleton); - return this; - } - - public IContainerRegistry RegisterSingleton(Type from, Type to, string name) - { - Instance.Register(from, to, Reuse.Singleton, serviceKey: name); - return this; - } - - public IContainerRegistry Register(Type from, Type to) - { - Instance.Register(from, to); - return this; - } - - public IContainerRegistry Register(Type from, Type to, string name) - { - Instance.Register(from, to, ifAlreadyRegistered: IfAlreadyRegistered.Replace, serviceKey: name); - return this; - } - - public IContainerRegistry RegisterMany(Type implementingType, params Type[] serviceTypes) - { - if(serviceTypes.Length == 0) - { - serviceTypes = implementingType.GetInterfaces(); - } - - Instance.RegisterMany(serviceTypes, implementingType, Reuse.Transient); - return this; - } - - public IContainerRegistry RegisterManySingleton(Type implementingType, params Type[] serviceTypes) - { - if (serviceTypes.Length == 0) - { - serviceTypes = implementingType.GetInterfaces(); - } - - Instance.RegisterMany(serviceTypes, implementingType, Reuse.Singleton); - return this; - } - - public bool IsRegistered(Type type) - { - return Instance.IsRegistered(type) || Instance.IsRegistered(type, factoryType: FactoryType.Wrapper); - } - - public bool IsRegistered(Type type, string name) - { - return Instance.IsRegistered(type, name) || Instance.IsRegistered(type, name, factoryType: FactoryType.Wrapper); - } - - public IContainerRegistry RegisterDelegate(Type serviceType, Func factoryMethod) - { - Instance.RegisterDelegate(serviceType, r => factoryMethod()); - return this; - } - - public IContainerRegistry RegisterDelegate(Type serviceType, Func factoryMethod) - { - Instance.RegisterDelegate(serviceType, factoryMethod); - return this; - } - - public IContainerRegistry RegisterDelegate(Type serviceType, Func factoryMethod) - { - Instance.RegisterDelegate(serviceType, factoryMethod); - return this; - } - - public IContainerRegistry RegisterSingletonFromDelegate(Type serviceType, Func factoryMethod) - { - Instance.RegisterDelegate(serviceType, r => factoryMethod(), Reuse.Singleton); - return this; - } - - public IContainerRegistry RegisterSingletonFromDelegate(Type serviceType, Func factoryMethod) - { - Instance.RegisterDelegate(serviceType, factoryMethod, Reuse.Singleton); - return this; - } - - public IContainerRegistry RegisterSingletonFromDelegate(Type serviceType, Func factoryMethod) - { - Instance.RegisterDelegate(serviceType, factoryMethod, Reuse.Singleton); - return this; - } + public static Rules DefaultRules => DryIocContainerExtension.DefaultRules; - public IContainerRegistry RegisterScoped(Type serviceType) => - RegisterScoped(serviceType, serviceType); + public static IContainerExtension Current => ContainerLocator.Current; - public IContainerRegistry RegisterScoped(Type serviceType, Type implementationType) - { - Instance.Register(serviceType, implementationType, Reuse.ScopedOrSingleton); - return this; - } + public static void Init() => + Init(DefaultRules); - public IContainerRegistry RegisterScopedFromDelegate(Type serviceType, Func factoryMethod) - { - Instance.RegisterDelegate(serviceType, r => factoryMethod(), Reuse.ScopedOrSingleton); - return this; - } + public static void Init(Rules rules) => + Init(new global::DryIoc.Container(rules)); - public IContainerRegistry RegisterScopedFromDelegate(Type serviceType, Func factoryMethod) + public static void Init(IContainer container) { - Instance.RegisterDelegate(serviceType, factoryMethod, Reuse.ScopedOrSingleton); - return this; + ContainerLocator.SetContainerExtension(() => new DryIocContainerExtension(container)); } - public IContainerRegistry RegisterScopedFromDelegate(Type serviceType, Func factoryMethod) - { - Instance.RegisterDelegate(serviceType, factoryMethod, Reuse.ScopedOrSingleton); - return this; - } - - void IScopeProvider.CreateScope() - { - CreateScopeInternal(); - } - - IServiceScope IServiceScopeFactory.CreateScope() => - CreateScopeInternal(); - - private IServiceScope CreateScopeInternal() - { - if(_currentScope != null) - { - _currentScope.Dispose(); - _currentScope = null; - GC.Collect(); - } - - _currentScope = new ServiceScope(Instance.OpenScope()); - return _currentScope; - } - - private class ServiceScope : IServiceScope - { - public ServiceScope(IResolverContext context) - { - Context = context; - } - - public IResolverContext Context { get; private set; } - - public IServiceProvider ServiceProvider => Context; - - public void Dispose() - { - if(Context != null) - { - Context.Dispose(); - Context = null; - } - - GC.Collect(); - } - } - - public object Resolve(Type type) => - Resolve(type, Array.Empty<(Type, object)>()); - - public object Resolve(Type type, string name) => - Resolve(type, name, Array.Empty<(Type, object)>()); - - public object Resolve(Type type, params (Type Type, object Instance)[] parameters) - { - try - { - var container = _currentScope?.Context ?? Instance; - return container.Resolve(type, args: parameters.Select(p => p.Instance).ToArray()); - } - catch (Exception ex) - { - throw new ContainerResolutionException(type, ex); - } - } - - public object Resolve(Type type, string name, params (Type Type, object Instance)[] parameters) - { - try - { - var container = _currentScope?.Context ?? Instance; - return container.Resolve(type, name, args: parameters.Select(p => p.Instance).ToArray()); - } - catch(Exception ex) - { - throw new ContainerResolutionException(type, name, ex); - } - } - - public object GetService(Type serviceType) + internal static void Reset() { - if (!IsRegistered(serviceType)) return null; - - return Resolve(serviceType); + ContainerLocator.ResetContainer(); + GC.Collect(int.MaxValue, GCCollectionMode.Forced); + GC.WaitForFullGCComplete(); } } } diff --git a/src/Prism.DryIoc.Extensions/Properties/AssemblyInfo.cs b/src/Prism.DryIoc.Extensions/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9d8b741 --- /dev/null +++ b/src/Prism.DryIoc.Extensions/Properties/AssemblyInfo.cs @@ -0,0 +1,5 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Prism.DryIoc.Extensions.Tests")] +[assembly: InternalsVisibleTo("Prism.DryIoc.Forms.Extended.Tests")] +[assembly: InternalsVisibleTo("Shiny.Prism.Tests")] diff --git a/src/Prism.Forms.Extended/Prism.Forms.Extended.csproj b/src/Prism.Forms.Extended/Prism.Forms.Extended.csproj index 5449156..bf99c4c 100644 --- a/src/Prism.Forms.Extended/Prism.Forms.Extended.csproj +++ b/src/Prism.Forms.Extended/Prism.Forms.Extended.csproj @@ -29,7 +29,7 @@ - + From f13c072505470907047d8e6d0450a11e492d3f93 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Thu, 27 Aug 2020 20:24:19 -0700 Subject: [PATCH 02/13] update DryIoc for Prism 8 use nerdbank --- Directory.build.props | 9 ++++----- version.json | 12 ++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 version.json diff --git a/Directory.build.props b/Directory.build.props index f832fd5..d981f6c 100644 --- a/Directory.build.props +++ b/Directory.build.props @@ -4,7 +4,6 @@ dansiegel © Dan Siegel. All rights reserved. - 8.0 en-US true snupkg @@ -28,12 +27,12 @@ $(IsPackable) false false - $(VersionPrefix).$(BUILD_BUILDNUMBER) - ci - pre - + + + +