diff --git a/src/Umbraco.Core/ObjectResolution/ContainerLazyManyObjectsResolver.cs b/src/Umbraco.Core/ObjectResolution/ContainerLazyManyObjectsResolver.cs deleted file mode 100644 index ba35324094..0000000000 --- a/src/Umbraco.Core/ObjectResolution/ContainerLazyManyObjectsResolver.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using LightInject; -using Umbraco.Core.Logging; - -namespace Umbraco.Core.ObjectResolution -{ - /// - /// A lazy many objects resolver that uses IoC - /// - /// - /// - public abstract class ContainerLazyManyObjectsResolver : LazyManyObjectsResolverBase - where TResolved : class - where TResolver : ResolverBase - { - protected IServiceContainer Container; - private object _locker = new object(); - private bool _isInitialized; - - internal ContainerLazyManyObjectsResolver(IServiceContainer container, ILogger logger, Func> typeListProducerList, ObjectLifetimeScope scope = ObjectLifetimeScope.Application) - : base(logger, typeListProducerList, scope) - { - if (container == null) throw new ArgumentNullException(nameof(container)); - Container = container; - - //Register ourselves in the case that a resolver instance should be injected someplace - Container.Register(factory => (TResolver)(object)this); - } - - /// - /// Ensures that the types are registered in the container before the values can be resolved - /// - /// - /// - /// A callback that executes after the types are registered, this allows for custom registrations for inheritors - /// - protected void EnsureTypesRegisterred(ObjectLifetimeScope scope, Action afterRegistered = null) - { - //Before we can do anything, the first time this happens we need to setup the container with the resolved types - LazyInitializer.EnsureInitialized(ref Container, ref _isInitialized, ref _locker, () => - { - foreach (var type in InstanceTypes) - Container.Register(type, GetLifetime(LifetimeScope)); - - afterRegistered?.Invoke(Container); - - return Container; - }); - } - - /// - /// Creates the instances from IoC - /// - /// A list of objects of type . - protected override IEnumerable CreateValues(ObjectLifetimeScope scope) - { - EnsureTypesRegisterred(scope); - - //NOTE: we ignore scope because objects are registered under this scope and not build based on the scope. - - return Container.GetAllInstances(); - } - - /// - /// Convert the ObjectLifetimeScope to ILifetime - /// - /// - /// - protected static ILifetime GetLifetime(ObjectLifetimeScope scope) - { - switch (scope) - { - case ObjectLifetimeScope.HttpRequest: - return new PerRequestLifeTime(); - case ObjectLifetimeScope.Application: - return new PerContainerLifetime(); - //case ObjectLifetimeScope.Transient: - default: - return null; - } - } - } -} \ No newline at end of file diff --git a/src/Umbraco.Core/ObjectResolution/LazyManyObjectsResolverbase.cs b/src/Umbraco.Core/ObjectResolution/LazyManyObjectsResolverbase.cs deleted file mode 100644 index 0a74e9b928..0000000000 --- a/src/Umbraco.Core/ObjectResolution/LazyManyObjectsResolverbase.cs +++ /dev/null @@ -1,273 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Web; -using Umbraco.Core.Logging; - -namespace Umbraco.Core.ObjectResolution -{ - /// - /// The base class for all lazy many-objects resolvers. - /// - /// The type of the concrete resolver class. - /// The type of the resolved objects. - /// - /// This is a special case resolver for when types get lazily resolved in order to resolve the actual types. This is useful - /// for when there is some processing overhead (i.e. Type finding in assemblies) to return the Types used to instantiate the instances. - /// In some these cases we don't want to have to type-find during application startup, only when we need to resolve the instances. - /// Important notes about this resolver: it does not support Insert or Remove and therefore does not support any ordering unless - /// the types are marked with the WeightedPluginAttribute. - /// - public abstract class LazyManyObjectsResolverBase : ManyObjectsResolverBase - where TResolved : class - where TResolver : ResolverBase - { - #region Constructors - - /// - /// Hack: This is purely here to allow for the lazy container resolver to be used, we'll need to refactor all of this slowly till we're - /// happy with the outcome - /// - /// - /// - /// - internal LazyManyObjectsResolverBase(ILogger logger, Func> typeListProducerList, ObjectLifetimeScope scope = ObjectLifetimeScope.Application) - : base(logger, scope) - { - if (typeListProducerList == null) throw new ArgumentNullException("typeListProducerList"); - _typeListProducerList.Add(typeListProducerList); - Initialize(); - } - - /// - /// Initializes a new instance of the class with an empty list of objects, - /// and an optional lifetime scope. - /// - /// - /// - /// The lifetime scope of instantiated objects, default is per Application. - /// If is per HttpRequest then there must be a current HttpContext. - /// is per HttpRequest but the current HttpContext is null. - protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, ObjectLifetimeScope scope = ObjectLifetimeScope.Application) - : base(serviceProvider, logger, scope) - { - Initialize(); - } - - protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, IEnumerable> lazyTypeList, ObjectLifetimeScope scope = ObjectLifetimeScope.Application) - : this(serviceProvider, logger, scope) - { - AddTypes(lazyTypeList); - } - - protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, Func> typeListProducerList, ObjectLifetimeScope scope = ObjectLifetimeScope.Application) - : this(serviceProvider, logger, scope) - { - _typeListProducerList.Add(typeListProducerList); - } - - protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, HttpContextBase httpContext, IEnumerable> lazyTypeList) - : this(serviceProvider, logger, httpContext) - { - AddTypes(lazyTypeList); - } - - protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, HttpContextBase httpContext, Func> typeListProducerList) - : this(serviceProvider, logger, httpContext) - { - _typeListProducerList.Add(typeListProducerList); - } - - #endregion - - private readonly List> _lazyTypeList = new List>(); - private readonly List>> _typeListProducerList = new List>>(); - private readonly List _excludedTypesList = new List(); - private Lazy> _resolvedTypes; - - /// - /// Initializes a new instance of the class with an empty list of objects, - /// with creation of objects based on an HttpRequest lifetime scope. - /// - /// - /// - /// The HttpContextBase corresponding to the HttpRequest. - /// is null. - protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, HttpContextBase httpContext) - : base(serviceProvider, logger, httpContext) - { - } - - /// - /// Initializes a new instance of the class with an initial list of object types, - /// and an optional lifetime scope. - /// - /// - /// - /// The list of object types. - /// The lifetime scope of instantiated objects, default is per Application. - /// If is per HttpRequest then there must be a current HttpContext. - /// is per HttpRequest but the current HttpContext is null. - protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, IEnumerable value, ObjectLifetimeScope scope = ObjectLifetimeScope.Application) - : base(serviceProvider, logger, value, scope) - { - } - - private void Initialize() - { - _resolvedTypes = new Lazy>(() => - { - var resolvedTypes = new List(); - - // get the types by evaluating the lazy & producers - var types = new List(); - types.AddRange(_lazyTypeList.Select(x => x.Value)); - types.AddRange(_typeListProducerList.SelectMany(x => x())); - - // we need to validate each resolved type now since we could - // not do it before evaluating the lazy & producers - foreach (var type in types.Where(x => _excludedTypesList.Contains(x) == false)) - { - AddValidAndNoDuplicate(resolvedTypes, type); - } - - return resolvedTypes; - }); - } - - /// - /// Gets a value indicating whether the resolver has resolved types to create instances from. - /// - /// To be used in unit tests. - public bool HasResolvedTypes - { - get { return _resolvedTypes.IsValueCreated; } - } - - /// - /// Gets the list of types to create instances from. - /// - /// When called, will get the types from the lazy list. - protected override IEnumerable InstanceTypes - { - get { return _resolvedTypes.Value; } - } - - /// - /// Ensures that type is valid and not a duplicate - /// then appends the type to the end of the list - /// - /// - /// - private void AddValidAndNoDuplicate(List list, Type type) - { - EnsureCorrectType(type); - if (list.Contains(type)) - { - throw new InvalidOperationException(string.Format( - "Type {0} is already in the collection of types.", type.FullName)); - } - list.Add(type); - } - - #region Types collection manipulation - - /// - /// Removes types from the list of types, once it has been lazily evaluated, and before actual objects are instanciated. - /// - /// The type to remove. - public override void RemoveType(Type value) - { - EnsureSupportsRemove(); - - _excludedTypesList.Add(value); - } - - /// - /// Lazily adds types from lazy types. - /// - /// The lazy types, to add. - protected void AddTypes(IEnumerable> types) - { - EnsureSupportsAdd(); - - using (Resolution.Configuration) - using (GetWriteLock()) - { - foreach (var t in types) - { - _lazyTypeList.Add(t); - } - } - } - - /// - /// Lazily adds types from a function producing types. - /// - /// The functions producing types, to add. - public void AddTypeListDelegate(Func> typeListProducer) - { - EnsureSupportsAdd(); - - using (Resolution.Configuration) - using (GetWriteLock()) - { - _typeListProducerList.Add(typeListProducer); - } - } - - /// - /// Lazily adds a type from a lazy type. - /// - /// The lazy type, to add. - public void AddType(Lazy value) - { - EnsureSupportsAdd(); - - using (Resolution.Configuration) - using (GetWriteLock()) - { - _lazyTypeList.Add(value); - } - } - - /// - /// Lazily adds a type from an actual type. - /// - /// The actual type, to add. - /// The type is converted to a lazy type. - public override void AddType(Type value) - { - AddType(new Lazy(() => value)); - } - - /// - /// Clears all lazy types - /// - public override void Clear() - { - EnsureSupportsClear(); - - using (Resolution.Configuration) - using (GetWriteLock()) - { - _lazyTypeList.Clear(); - } - } - - #endregion - - #region Types collection manipulation support - - /// - /// Gets a false value indicating that the resolver does NOT support inserting types. - /// - protected override bool SupportsInsert - { - get { return false; } - } - - #endregion - } -} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index b24ca0a67a..2d338e4363 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -306,7 +306,6 @@ - @@ -1012,7 +1011,6 @@ -