This commit is contained in:
Stephan
2018-06-29 13:17:46 +02:00
parent 3bd7eedf03
commit 9bc8ace8db
12 changed files with 170 additions and 92 deletions

View File

@@ -22,6 +22,7 @@ namespace Umbraco.Core.Composing.CompositionRoots
// register cache helpers
// the main cache helper is registered by CoreBootManager and is used by most repositories
// the disabled one is used by those repositories that have an annotated ctor parameter
// fixme refactor: use a DisabledCacheHelper class (or interface?) so that injection does not depend on name and we can have simple ctor injection
container.RegisterSingleton(factory => CacheHelper.CreateDisabledCacheHelper(), DisabledCache);
// resolve ctor dependency from GetInstance() runtimeArguments, if possible - 'factory' is

View File

@@ -6,16 +6,62 @@ using System.Threading.Tasks;
namespace Umbraco.Core.Composing
{
// fixme - must document!
/// <summary>
/// Defines a container for Umbraco.
/// </summary>
public interface IContainer
{
T TryGetInstance<T>();
/// <summary>
/// Gets an instance.
/// </summary>
/// <typeparam name="T">The type of the instance.</typeparam>
/// <returns>An instance of the specified type.</returns>
/// <remarks>Throws an exception if the container failed to get an instance of the specified type.</remarks>
T GetInstance<T>();
object GetInstance(Type parameterType);
object ConcreteContainer { get; }
/// <summary>
/// Gets an instance.
/// </summary>
/// <param name="type">The type of the instance.</param>
/// <returns>An instance of the specified type.</returns>
/// <remarks>Throws an exception if the container failed to get an instance of the specified type.</remarks>
object GetInstance(Type type);
/// <summary>
/// Tries to get an instance.
/// </summary>
/// <typeparam name="T">The type of the instance.</typeparam>
/// <returns>An instance of the specified type, or null.</returns>
/// <remarks>Returns null if the container does not know how to get an instance
/// of the specified type. Throws an exception if the container does know how
/// to get an instance of the specified type, but failed to do so.</remarks>
T TryGetInstance<T>();
// fixme document
T GetInstance<T>(object[] args);
// fixme register direct type?
// fixme register an instance?
void RegisterSingleton<T>(Func<IContainer, T> factory);
void Register<T>(Func<IContainer, T> factory);
void Register<T, TService>(Func<IContainer, T, TService> factory);
/// <summary>
/// Registers and instanciates a collection builder.
/// </summary>
/// <typeparam name="T">The type of the collection builder.</typeparam>
/// <returns>A collection builder of the specified type.</returns>
T RegisterCollectionBuilder<T>();
T GetInstance<T>(object[] args);
// fixme move away!
object ConcreteContainer { get; }
}
// fixme would be nicer
//public interface IContainer<T> : IContainer
//{
// T ConcreteContainer { get; }
//}
}

View File

@@ -1,61 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LightInject;
namespace Umbraco.Core.Composing.LightInject
{
public class ContainerAdapter : IContainer
/// <summary>
/// Implements <see cref="IContainer"/> for LightInject.
/// </summary>
public class ContainerAdapter : IContainer // fixme rename LightInjectContainer?
{
private readonly IServiceContainer container;
public object ConcreteContainer => container;
public void RegisterSingleton<T>(Func<IContainer, T> factory)
{
container.RegisterSingleton(f => factory(this));
}
public void Register<T>(Func<IContainer, T> factory)
{
container.Register(f => factory(this));
}
public void Register<T, TService>(Func<IContainer, T, TService> factory)
{
container.Register<T, TService>((f, x) => factory(this, x));
}
public T RegisterCollectionBuilder<T>()
{
return container.RegisterCollectionBuilder<T>();
}
private readonly IServiceContainer _container;
/// <summary>
/// Initializes a new instance of the <see cref="ContainerAdapter"/> with a LightInject container.
/// </summary>
public ContainerAdapter(IServiceContainer container)
{
this.container = container;
_container = container;
}
// fixme
public object ConcreteContainer => _container;
/// <inheritdoc />
public void RegisterSingleton<T>(Func<IContainer, T> factory)
=> _container.RegisterSingleton(f => factory(this));
/// <inheritdoc />
public void Register<T>(Func<IContainer, T> factory)
=> _container.Register(f => factory(this));
/// <inheritdoc />
public void Register<T, TService>(Func<IContainer, T, TService> factory)
=> _container.Register<T, TService>((f, x) => factory(this, x));
/// <inheritdoc />
public T RegisterCollectionBuilder<T>()
=> _container.RegisterCollectionBuilder<T>();
/// <inheritdoc />
public T TryGetInstance<T>()
{
return container.TryGetInstance<T>();
}
=> _container.TryGetInstance<T>();
/// <inheritdoc />
public T GetInstance<T>()
{
return container.GetInstance<T>();
}
=> _container.GetInstance<T>();
/// <inheritdoc />
public T GetInstance<T>(object[] args)
{
return (T)container.GetInstance(typeof(T), args);
}
=> (T) _container.GetInstance(typeof(T), args);
/// <inheritdoc />
public object GetInstance(Type type)
{
return container.GetInstance(type);
}
=> _container.GetInstance(type);
}
}

View File

@@ -44,6 +44,7 @@ namespace Umbraco.Core.Composing
container.ScopeManagerProvider = new MixedLightInjectScopeManagerProvider();
// self-register
// fixme - WHERE is this used, and should it be a generic container, not LightInject's?
container.Register<IServiceContainer>(_ => container);
// configure the current container