Resvolution - Kill

This commit is contained in:
Stephan
2016-08-19 11:14:35 +02:00
parent 1b9aa68bb2
commit efcd1d5483
3 changed files with 0 additions and 360 deletions

View File

@@ -1,85 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading;
using LightInject;
using Umbraco.Core.Logging;
namespace Umbraco.Core.ObjectResolution
{
/// <summary>
/// A lazy many objects resolver that uses IoC
/// </summary>
/// <typeparam name="TResolver"></typeparam>
/// <typeparam name="TResolved"></typeparam>
public abstract class ContainerLazyManyObjectsResolver<TResolver, TResolved> : LazyManyObjectsResolverBase<TResolver, TResolved>
where TResolved : class
where TResolver : ResolverBase
{
protected IServiceContainer Container;
private object _locker = new object();
private bool _isInitialized;
internal ContainerLazyManyObjectsResolver(IServiceContainer container, ILogger logger, Func<IEnumerable<Type>> 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);
}
/// <summary>
/// Ensures that the types are registered in the container before the values can be resolved
/// </summary>
/// <param name="scope"></param>
/// <param name="afterRegistered">
/// A callback that executes after the types are registered, this allows for custom registrations for inheritors
/// </param>
protected void EnsureTypesRegisterred(ObjectLifetimeScope scope, Action<IServiceContainer> 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;
});
}
/// <summary>
/// Creates the instances from IoC
/// </summary>
/// <returns>A list of objects of type <typeparamref name="TResolved"/>.</returns>
protected override IEnumerable<TResolved> 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<TResolved>();
}
/// <summary>
/// Convert the ObjectLifetimeScope to ILifetime
/// </summary>
/// <param name="scope"></param>
/// <returns></returns>
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;
}
}
}
}

View File

@@ -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
{
/// <summary>
/// The base class for all lazy many-objects resolvers.
/// </summary>
/// <typeparam name="TResolver">The type of the concrete resolver class.</typeparam>
/// <typeparam name="TResolved">The type of the resolved objects.</typeparam>
/// <remarks>
/// <para>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.</para>
/// <para>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.</para>
/// </remarks>
public abstract class LazyManyObjectsResolverBase<TResolver, TResolved> : ManyObjectsResolverBase<TResolver, TResolved>
where TResolved : class
where TResolver : ResolverBase
{
#region Constructors
/// <summary>
/// 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
/// </summary>
/// <param name="logger"></param>
/// <param name="typeListProducerList"></param>
/// <param name="scope"></param>
internal LazyManyObjectsResolverBase(ILogger logger, Func<IEnumerable<Type>> typeListProducerList, ObjectLifetimeScope scope = ObjectLifetimeScope.Application)
: base(logger, scope)
{
if (typeListProducerList == null) throw new ArgumentNullException("typeListProducerList");
_typeListProducerList.Add(typeListProducerList);
Initialize();
}
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolver, TResolved}"/> class with an empty list of objects,
/// and an optional lifetime scope.
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="logger"></param>
/// <param name="scope">The lifetime scope of instantiated objects, default is per Application.</param>
/// <remarks>If <paramref name="scope"/> is per HttpRequest then there must be a current HttpContext.</remarks>
/// <exception cref="InvalidOperationException"><paramref name="scope"/> is per HttpRequest but the current HttpContext is null.</exception>
protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, ObjectLifetimeScope scope = ObjectLifetimeScope.Application)
: base(serviceProvider, logger, scope)
{
Initialize();
}
protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, IEnumerable<Lazy<Type>> lazyTypeList, ObjectLifetimeScope scope = ObjectLifetimeScope.Application)
: this(serviceProvider, logger, scope)
{
AddTypes(lazyTypeList);
}
protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, Func<IEnumerable<Type>> typeListProducerList, ObjectLifetimeScope scope = ObjectLifetimeScope.Application)
: this(serviceProvider, logger, scope)
{
_typeListProducerList.Add(typeListProducerList);
}
protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, HttpContextBase httpContext, IEnumerable<Lazy<Type>> lazyTypeList)
: this(serviceProvider, logger, httpContext)
{
AddTypes(lazyTypeList);
}
protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, HttpContextBase httpContext, Func<IEnumerable<Type>> typeListProducerList)
: this(serviceProvider, logger, httpContext)
{
_typeListProducerList.Add(typeListProducerList);
}
#endregion
private readonly List<Lazy<Type>> _lazyTypeList = new List<Lazy<Type>>();
private readonly List<Func<IEnumerable<Type>>> _typeListProducerList = new List<Func<IEnumerable<Type>>>();
private readonly List<Type> _excludedTypesList = new List<Type>();
private Lazy<List<Type>> _resolvedTypes;
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolver, TResolved}"/> class with an empty list of objects,
/// with creation of objects based on an HttpRequest lifetime scope.
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="logger"></param>
/// <param name="httpContext">The HttpContextBase corresponding to the HttpRequest.</param>
/// <exception cref="ArgumentNullException"><paramref name="httpContext"/> is <c>null</c>.</exception>
protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, HttpContextBase httpContext)
: base(serviceProvider, logger, httpContext)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ManyObjectsResolverBase{TResolver, TResolved}"/> class with an initial list of object types,
/// and an optional lifetime scope.
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="logger"></param>
/// <param name="value">The list of object types.</param>
/// <param name="scope">The lifetime scope of instantiated objects, default is per Application.</param>
/// <remarks>If <paramref name="scope"/> is per HttpRequest then there must be a current HttpContext.</remarks>
/// <exception cref="InvalidOperationException"><paramref name="scope"/> is per HttpRequest but the current HttpContext is null.</exception>
protected LazyManyObjectsResolverBase(IServiceProvider serviceProvider, ILogger logger, IEnumerable<Type> value, ObjectLifetimeScope scope = ObjectLifetimeScope.Application)
: base(serviceProvider, logger, value, scope)
{
}
private void Initialize()
{
_resolvedTypes = new Lazy<List<Type>>(() =>
{
var resolvedTypes = new List<Type>();
// get the types by evaluating the lazy & producers
var types = new List<Type>();
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;
});
}
/// <summary>
/// Gets a value indicating whether the resolver has resolved types to create instances from.
/// </summary>
/// <remarks>To be used in unit tests.</remarks>
public bool HasResolvedTypes
{
get { return _resolvedTypes.IsValueCreated; }
}
/// <summary>
/// Gets the list of types to create instances from.
/// </summary>
/// <remarks>When called, will get the types from the lazy list.</remarks>
protected override IEnumerable<Type> InstanceTypes
{
get { return _resolvedTypes.Value; }
}
/// <summary>
/// Ensures that type is valid and not a duplicate
/// then appends the type to the end of the list
/// </summary>
/// <param name="list"></param>
/// <param name="type"></param>
private void AddValidAndNoDuplicate(List<Type> 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
/// <summary>
/// Removes types from the list of types, once it has been lazily evaluated, and before actual objects are instanciated.
/// </summary>
/// <param name="value">The type to remove.</param>
public override void RemoveType(Type value)
{
EnsureSupportsRemove();
_excludedTypesList.Add(value);
}
/// <summary>
/// Lazily adds types from lazy types.
/// </summary>
/// <param name="types">The lazy types, to add.</param>
protected void AddTypes(IEnumerable<Lazy<Type>> types)
{
EnsureSupportsAdd();
using (Resolution.Configuration)
using (GetWriteLock())
{
foreach (var t in types)
{
_lazyTypeList.Add(t);
}
}
}
/// <summary>
/// Lazily adds types from a function producing types.
/// </summary>
/// <param name="typeListProducer">The functions producing types, to add.</param>
public void AddTypeListDelegate(Func<IEnumerable<Type>> typeListProducer)
{
EnsureSupportsAdd();
using (Resolution.Configuration)
using (GetWriteLock())
{
_typeListProducerList.Add(typeListProducer);
}
}
/// <summary>
/// Lazily adds a type from a lazy type.
/// </summary>
/// <param name="value">The lazy type, to add.</param>
public void AddType(Lazy<Type> value)
{
EnsureSupportsAdd();
using (Resolution.Configuration)
using (GetWriteLock())
{
_lazyTypeList.Add(value);
}
}
/// <summary>
/// Lazily adds a type from an actual type.
/// </summary>
/// <param name="value">The actual type, to add.</param>
/// <remarks>The type is converted to a lazy type.</remarks>
public override void AddType(Type value)
{
AddType(new Lazy<Type>(() => value));
}
/// <summary>
/// Clears all lazy types
/// </summary>
public override void Clear()
{
EnsureSupportsClear();
using (Resolution.Configuration)
using (GetWriteLock())
{
_lazyTypeList.Clear();
}
}
#endregion
#region Types collection manipulation support
/// <summary>
/// Gets a <c>false</c> value indicating that the resolver does NOT support inserting types.
/// </summary>
protected override bool SupportsInsert
{
get { return false; }
}
#endregion
}
}

View File

@@ -306,7 +306,6 @@
<Compile Include="Models\ContentTypeAvailableCompositionsResults.cs" />
<Compile Include="Models\EntityContainer.cs" />
<Compile Include="Models\Rdbms\LockDto.cs" />
<Compile Include="ObjectResolution\ContainerLazyManyObjectsResolver.cs" />
<Compile Include="ObjectResolution\ContainerManyObjectsResolver.cs" />
<Compile Include="Models\Identity\IdentityModelMappings.cs" />
<Compile Include="Models\Identity\IdentityUser.cs" />
@@ -1012,7 +1011,6 @@
<Compile Include="Persistence\Repositories\UserTypeRepository.cs" />
<Compile Include="Persistence\Repositories\VersionableRepositoryBase.cs" />
<Compile Include="Persistence\RepositoryFactory.cs" />
<Compile Include="ObjectResolution\LazyManyObjectsResolverBase.cs" />
<Compile Include="Persistence\SqlSyntax\ColumnInfo.cs" />
<Compile Include="Persistence\SqlSyntax\DbTypes.cs" />
<Compile Include="Persistence\SqlSyntax\ISqlSyntaxProvider.cs" />