Skip to content

Commit

Permalink
Closed #90
Browse files Browse the repository at this point in the history
Added design time Lifetime Manager validation
  • Loading branch information
ENikS committed Jan 18, 2019
1 parent db0615d commit 23b26c3
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 53 deletions.
14 changes: 7 additions & 7 deletions src/Extensions/Container/UnityContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static IUnityContainer RegisterType<T>(this IUnityContainer container, pa
/// of the returned instance.</param>
/// <param name="injectionMembers">Injection configuration objects.</param>
/// <returns>The <see cref="Unity.IUnityContainer"/> object that this method was called on.</returns>
public static IUnityContainer RegisterType<T>(this IUnityContainer container, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
public static IUnityContainer RegisterType<T>(this IUnityContainer container, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return (container ?? throw new ArgumentNullException(nameof(container))).RegisterType((Type)null, typeof(T), null, lifetimeManager, injectionMembers);
}
Expand Down Expand Up @@ -69,7 +69,7 @@ public static IUnityContainer RegisterType<T>(this IUnityContainer container, st
/// of the returned instance.</param>
/// <param name="injectionMembers">Injection configuration objects.</param>
/// <returns>The <see cref="Unity.IUnityContainer"/> object that this method was called on.</returns>
public static IUnityContainer RegisterType<T>(this IUnityContainer container, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
public static IUnityContainer RegisterType<T>(this IUnityContainer container, string name, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return (container ?? throw new ArgumentNullException(nameof(container))).RegisterType((Type)null, typeof(T), name, lifetimeManager, injectionMembers);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public static IUnityContainer RegisterType<T>(this IUnityContainer container, st
/// of the returned instance.</param>
/// <param name="injectionMembers">Injection configuration objects.</param>
/// <returns>The <see cref="Unity.IUnityContainer"/> object that this method was called on.</returns>
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom
{
return (container ?? throw new ArgumentNullException(nameof(container))).RegisterType(typeof(TFrom), typeof(TTo), null, lifetimeManager, injectionMembers);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public static IUnityContainer RegisterType<T>(this IUnityContainer container, st
/// of the returned instance.</param>
/// <param name="injectionMembers">Injection configuration objects.</param>
/// <returns>The <see cref="Unity.IUnityContainer"/> object that this method was called on.</returns>
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, string name, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom
{
return (container ?? throw new ArgumentNullException(nameof(container))).RegisterType(typeof(TFrom), typeof(TTo), name, lifetimeManager, injectionMembers);
}
Expand Down Expand Up @@ -245,7 +245,7 @@ public static IUnityContainer RegisterType(this IUnityContainer container, Type
/// of the returned instance.</param>
/// <param name="injectionMembers">Injection configuration objects.</param>
/// <returns>The <see cref="Unity.IUnityContainer"/> object that this method was called on.</returns>
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return (container ?? throw new ArgumentNullException(nameof(container))).RegisterType((Type)null, t, null, lifetimeManager, injectionMembers);
}
Expand Down Expand Up @@ -275,7 +275,7 @@ public static IUnityContainer RegisterType(this IUnityContainer container, Type
/// of the returned instance.</param>
/// <param name="injectionMembers">Injection configuration objects.</param>
/// <returns>The <see cref="Unity.IUnityContainer"/> object that this method was called on.</returns>
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, string name, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return (container ?? throw new ArgumentNullException(nameof(container))).RegisterType((Type)null, t, name, lifetimeManager, injectionMembers);
}
Expand Down Expand Up @@ -333,7 +333,7 @@ public static IUnityContainer RegisterType(this IUnityContainer container, Type
/// of the returned instance.</param>
/// <param name="injectionMembers">Injection configuration objects.</param>
/// <returns>The <see cref="Unity.IUnityContainer"/> object that this method was called on.</returns>
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return (container ?? throw new ArgumentNullException(nameof(container))).RegisterType(from, to, null, lifetimeManager, injectionMembers);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Extensions/Lifetime/TypeLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ namespace Unity
{
public static class TypeLifetime
{
public static LifetimeManager External => new ExternallyControlledLifetimeManager();
public static ITypeLifetimeManager External => new ExternallyControlledLifetimeManager();

public static LifetimeManager Singleton => new SingletonLifetimeManager();
public static ITypeLifetimeManager Singleton => new SingletonLifetimeManager();

public static LifetimeManager PerContainer => new ContainerControlledLifetimeManager();
public static ITypeLifetimeManager PerContainer => new ContainerControlledLifetimeManager();

public static LifetimeManager Hierarchical => new HierarchicalLifetimeManager();
public static ITypeLifetimeManager Hierarchical => new HierarchicalLifetimeManager();

public static LifetimeManager PerResolve => new PerResolveLifetimeManager();
public static ITypeLifetimeManager PerResolve => new PerResolveLifetimeManager();

public static LifetimeManager PerThread => new PerThreadLifetimeManager();
public static ITypeLifetimeManager PerThread => new PerThreadLifetimeManager();

public static LifetimeManager Transient { get; } = new TransientLifetimeManager();
public static ITypeLifetimeManager Transient { get; } = new TransientLifetimeManager();

public static LifetimeManager PerContainerTransient => new ContainerControlledTransientManager();
public static ITypeLifetimeManager PerContainerTransient => new ContainerControlledTransientManager();
}
}
2 changes: 1 addition & 1 deletion src/IUnityContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface IUnityContainer : IDisposable
/// registration and instead will use registration for <paramref name="typeTo"/> type to create object.</remarks>
/// <returns>The <see cref="IUnityContainer"/> object that this method was called on.</returns>
/// <exception cref="InvalidRegistrationException"></exception>
IUnityContainer RegisterType(Type typeFrom, Type typeTo, string name, ILifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);
IUnityContainer RegisterType(Type typeFrom, Type typeTo, string name, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);


/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/IUnityContainerAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IUnityContainerAsync : IDisposable
/// <param name="lifetimeManager">WithLifetime manager that will be responsible for managing created object's lifetime.</param>
/// <param name="injectionMembers">Injection configuration objects.</param>
/// <returns></returns>
IUnityContainer RegisterType(IEnumerable<Type> interfaces, Type type, string name, ILifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);
IUnityContainer RegisterType(IEnumerable<Type> interfaces, Type type, string name, ITypeLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);


/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Lifetime/Abstracts/IFactoryLifetimeManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Unity.Lifetime
{
public interface IFactoryLifetimeManager : ILifetimeManager
public interface IFactoryLifetimeManager
{
}
}
2 changes: 1 addition & 1 deletion src/Lifetime/Abstracts/IInstanceLifetimeManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Unity.Lifetime
{
public interface IInstanceLifetimeManager : ILifetimeManager
public interface IInstanceLifetimeManager
{
}
}
25 changes: 0 additions & 25 deletions src/Lifetime/Abstracts/ILifetimeManager.cs

This file was deleted.

6 changes: 6 additions & 0 deletions src/Lifetime/Abstracts/ITypeLifetimeManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Unity.Lifetime
{
public interface ITypeLifetimeManager
{
}
}
2 changes: 1 addition & 1 deletion src/Lifetime/Abstracts/LifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Unity.Lifetime
/// Base class for WithLifetime managers - classes that control how
/// and when instances are created by the Unity container.
/// </summary>
public abstract class LifetimeManager : ILifetimeManager
public abstract class LifetimeManager
{
public virtual bool InUse { get; set; }

Expand Down
3 changes: 2 additions & 1 deletion src/Lifetime/Managers/ContainerControlledLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace Unity.Lifetime
/// </summary>
public class ContainerControlledLifetimeManager : SynchronizedLifetimeManager,
IInstanceLifetimeManager,
IFactoryLifetimeManager
IFactoryLifetimeManager,
ITypeLifetimeManager
{
#region Fields

Expand Down
3 changes: 2 additions & 1 deletion src/Lifetime/Managers/ContainerControlledTransientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Unity.Lifetime
/// is disposed all these objects are disposed as well.
/// </summary>
public class ContainerControlledTransientManager : LifetimeManager,
IFactoryLifetimeManager
IFactoryLifetimeManager,
ITypeLifetimeManager
{
public override void SetValue(object newValue, ILifetimeContainer container = null)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Lifetime/Managers/ExternallyControlledLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Unity.Lifetime
/// </summary>
public class ExternallyControlledLifetimeManager : LifetimeManager,
IInstanceLifetimeManager,
IFactoryLifetimeManager
IFactoryLifetimeManager,
ITypeLifetimeManager
{
private WeakReference _value = new WeakReference(null);

Expand Down
3 changes: 2 additions & 1 deletion src/Lifetime/Managers/HierarchicalLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace Unity.Lifetime
/// of the object, instead of sharing one in the common parent.
/// </summary>
public class HierarchicalLifetimeManager : SynchronizedLifetimeManager,
IFactoryLifetimeManager
IFactoryLifetimeManager,
ITypeLifetimeManager
{
#region Fields

Expand Down
3 changes: 2 additions & 1 deletion src/Lifetime/Managers/PerResolveLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
/// </summary>
public class PerResolveLifetimeManager : LifetimeManager,
IInstanceLifetimeManager,
IFactoryLifetimeManager
IFactoryLifetimeManager,
ITypeLifetimeManager
{
protected object value;

Expand Down
3 changes: 2 additions & 1 deletion src/Lifetime/Managers/PerThreadLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace Unity.Lifetime
/// </para>
/// </remarks>
public class PerThreadLifetimeManager : LifetimeManager,
IFactoryLifetimeManager
IFactoryLifetimeManager,
ITypeLifetimeManager
{
[ThreadStatic]
private static Dictionary<Guid, object> _values;
Expand Down
3 changes: 2 additions & 1 deletion src/Lifetime/Managers/SingletonLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace Unity.Lifetime
/// </summary>
public class SingletonLifetimeManager : SynchronizedLifetimeManager,
IInstanceLifetimeManager,
IFactoryLifetimeManager
IFactoryLifetimeManager,
ITypeLifetimeManager
{
#region Fields

Expand Down
3 changes: 2 additions & 1 deletion src/Lifetime/Managers/TransientLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/// thus ensuring that instances are created new every time.
/// </summary>
public class TransientLifetimeManager : LifetimeManager,
IFactoryLifetimeManager
IFactoryLifetimeManager,
ITypeLifetimeManager
{
public static TransientLifetimeManager Instance = new TransientLifetimeManager();

Expand Down

0 comments on commit 23b26c3

Please sign in to comment.