Skip to content

Commit

Permalink
One test to go
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Jan 6, 2018
1 parent 614adb0 commit bb5e628
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 270 deletions.
74 changes: 0 additions & 74 deletions src/Aggregate.cs

This file was deleted.

7 changes: 4 additions & 3 deletions src/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.DependencyInjection;
using Unity.Injection;
using Unity.Lifetime;
using Unity.Microsoft.DependencyInjection.Lifetime;

namespace Unity.Microsoft.DependencyInjection
{
Expand All @@ -13,7 +14,7 @@ internal static class Configuration

internal static IUnityContainer AddServices(this IUnityContainer container, IServiceCollection services)
{
var lifetime = container.Configure<MDIExtension>()
var lifetime = container.Configure<MdiExtension>()
.Lifetime;

foreach (var group in services.GroupBy(serviceDescriptor => serviceDescriptor.ServiceType,
Expand All @@ -24,7 +25,7 @@ internal static IUnityContainer AddServices(this IUnityContainer container, ISer
for (var i = 0; i < group.Length - 1; i++)
{
var descriptor = group[i];
container.Register(descriptor, descriptor.GetRegistrationName(), lifetime);
container.Register(descriptor, Guid.NewGuid().ToString(), lifetime);
}

// Register default types
Expand Down Expand Up @@ -79,7 +80,7 @@ internal static LifetimeManager GetLifetime(this ServiceDescriptor serviceDescri
case ServiceLifetime.Scoped:
return new HierarchicalLifetimeManager();
case ServiceLifetime.Singleton:
return new InjectionSingletonLifetimeManager(lifetime);
return new ContainerControlledLifetimeManager();
case ServiceLifetime.Transient:
return new InjectionTransientLifetimeManager();
default:
Expand Down
13 changes: 0 additions & 13 deletions src/Lifetime/InjectionScopeLifetimeManager.cs

This file was deleted.

102 changes: 17 additions & 85 deletions src/Lifetime/InjectionSingletonLifetimeManager.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
using System;
using System.Threading;
using Unity.Exceptions;
using Unity.Lifetime;

namespace Unity.Microsoft.DependencyInjection
namespace Unity.Microsoft.DependencyInjection.Lifetime
{
public class InjectionSingletonLifetimeManager : LifetimeManager, IRequiresRecovery
public class InjectionSingletonLifetimeManager : ContainerControlledLifetimeManager
{
#region Fields

protected object _value;
private ILifetimeContainer _lifetime;
private readonly object _lockObj = new object();


#endregion


Expand All @@ -22,100 +18,36 @@ public InjectionSingletonLifetimeManager(ILifetimeContainer lifetime)
}


/// <summary>
/// Retrieve a value from the backing store associated with this Lifetime policy.
/// </summary>
/// <returns>the object desired, or null if no such object is currently stored.</returns>
/// <remarks>Calls to this method acquire a lock which is released only if a non-null value
/// has been set for the lifetime manager.</remarks>
public override object GetValue(ILifetimeContainer container = null)
protected override void SynchronizedSetValue(object newValue, ILifetimeContainer container = null)
{
Monitor.Enter(_lockObj);
var result = SynchronizedGetValue(container);
if (result != null)
{
Monitor.Exit(_lockObj);
}
return result;
base.SynchronizedSetValue(newValue, container);
_lifetime.Add(new DisposableAction(() => RemoveValue(_lifetime)));
}

/// <summary>
/// Performs the actual retrieval of a value from the backing store associated
/// with this Lifetime policy.
/// </summary>
/// <returns>the object desired, or null if no such object is currently stored.</returns>
/// <remarks>This method is invoked by <see cref="SynchronizedLifetimeManager.GetValue"/>
/// after it has acquired its lock.</remarks>
protected virtual object SynchronizedGetValue(ILifetimeContainer container)
protected override LifetimeManager OnCreateLifetimeManager()
{
return _value;
return new InjectionSingletonLifetimeManager(_lifetime);
}


/// <summary>
/// Stores the given value into backing store for retrieval later.
/// </summary>
/// <param name="newValue">The object being stored.</param>
/// <param name="container">The container this value belongs to.</param>
/// <remarks>Setting a value will attempt to release the lock acquired by
/// <see cref="SynchronizedLifetimeManager.GetValue"/>.</remarks>
public override void SetValue(object newValue, ILifetimeContainer container = null)
{
SynchronizedSetValue(newValue, container);
TryExit();
}

/// <summary>
/// Performs the actual storage of the given value into backing store for retrieval later.
/// </summary>
/// <param name="newValue">The object being stored.</param>
/// <param name="container"></param>
/// <remarks>This method is invoked by <see cref="SynchronizedLifetimeManager.SetValue"/>
/// before releasing its lock.</remarks>
protected virtual void SynchronizedSetValue(object newValue, ILifetimeContainer container)
{
_value = newValue;
if (_value is IDisposable disposable) _lifetime.Add(disposable);
}
#region Nested Types

/// <summary>
/// A method that does whatever is needed to clean up
/// as part of cleaning up after an exception.
/// </summary>
/// <remarks>
/// Don't do anything that could throw in this method,
/// it will cause later recover operations to get skipped
/// and play real havoc with the stack trace.
/// </remarks>
public void Recover()
private class DisposableAction : IDisposable
{
TryExit();
}
private readonly Action _action;

protected virtual void TryExit()
{
#if !NET40
// Prevent first chance exception when abandoning a lock that has not been entered
if (!Monitor.IsEntered(_lockObj)) return;
#endif
try
public DisposableAction(Action action)
{
Monitor.Exit(_lockObj);
_action = action ?? throw new ArgumentNullException(nameof(action));
}
catch (SynchronizationLockException)

public void Dispose()
{
// Noop here - we don't hold the lock and that's ok.
_action();
}
}

public override void RemoveValue(ILifetimeContainer container = null)
{
TryExit();
}
#endregion

protected override LifetimeManager OnCreateLifetimeManager()
{
return new InjectionSingletonLifetimeManager(_lifetime);
}
}
}
2 changes: 1 addition & 1 deletion src/Lifetime/InjectionTransientLifetimeManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Unity.Lifetime;

namespace Unity.Microsoft.DependencyInjection
namespace Unity.Microsoft.DependencyInjection.Lifetime
{
/// <summary>
/// A special lifetime manager which works like <see cref="TransienLifetimeManager"/>,
Expand Down
3 changes: 2 additions & 1 deletion src/Extension/MDIExtension.cs → src/MDIExtension.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Unity.Extension;
using Unity.Lifetime;
using Unity.Microsoft.DependencyInjection.Policy;
using Unity.Policy;

namespace Unity.Microsoft.DependencyInjection
{
internal class MDIExtension : UnityContainerExtension
internal class MdiExtension : UnityContainerExtension
{
protected override void Initialize()
{
Expand Down
13 changes: 7 additions & 6 deletions src/Policy/ConstructorSelectorPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Unity.Policy;
using Unity.ObjectBuilder.BuildPlan.Selection;
using Unity.Attributes;
using Unity.Builder;
using Unity.Builder.Selection;
using Unity.Attributes;
using Unity.ObjectBuilder.BuildPlan.Selection;
using Unity.Policy;
using Unity.ResolverPolicy;

namespace Unity.Microsoft.DependencyInjection
namespace Unity.Microsoft.DependencyInjection.Policy
{
public class ConstructorSelectorPolicy : IConstructorSelectorPolicy
{
DefaultUnityConstructorSelectorPolicy dependency = new DefaultUnityConstructorSelectorPolicy();
private readonly DefaultUnityConstructorSelectorPolicy _dependency = new DefaultUnityConstructorSelectorPolicy();

/// <summary>
/// Choose the constructor to call for the given type.
Expand All @@ -28,7 +28,7 @@ public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyLis
ConstructorInfo ctor = FindDependencyConstructor<DependencyAttribute>(context);
if (ctor != null)
return CreateSelectedConstructor(ctor);
return dependency.SelectConstructor(context, resolverPolicyDestination);
return _dependency.SelectConstructor(context, resolverPolicyDestination);
}

private ConstructorInfo FindDependencyConstructor<T>(IBuilderContext context)
Expand Down Expand Up @@ -57,6 +57,7 @@ private static ConstructorInfo FindSingleConstructor(IEnumerable<ConstructorInfo
{
if (constructors.Count() == 1)
return constructors.First();

return null;
}

Expand Down
15 changes: 0 additions & 15 deletions src/ServiceCollectionExtensions.cs

This file was deleted.

46 changes: 0 additions & 46 deletions src/ServiceDescriptorExtensions.cs

This file was deleted.

Loading

0 comments on commit bb5e628

Please sign in to comment.