Skip to content

Commit bb5e628

Browse files
committed
One test to go
1 parent 614adb0 commit bb5e628

11 files changed

+72
-270
lines changed

src/Aggregate.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/Configuration.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.DependencyInjection;
66
using Unity.Injection;
77
using Unity.Lifetime;
8+
using Unity.Microsoft.DependencyInjection.Lifetime;
89

910
namespace Unity.Microsoft.DependencyInjection
1011
{
@@ -13,7 +14,7 @@ internal static class Configuration
1314

1415
internal static IUnityContainer AddServices(this IUnityContainer container, IServiceCollection services)
1516
{
16-
var lifetime = container.Configure<MDIExtension>()
17+
var lifetime = container.Configure<MdiExtension>()
1718
.Lifetime;
1819

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

3031
// Register default types
@@ -79,7 +80,7 @@ internal static LifetimeManager GetLifetime(this ServiceDescriptor serviceDescri
7980
case ServiceLifetime.Scoped:
8081
return new HierarchicalLifetimeManager();
8182
case ServiceLifetime.Singleton:
82-
return new InjectionSingletonLifetimeManager(lifetime);
83+
return new ContainerControlledLifetimeManager();
8384
case ServiceLifetime.Transient:
8485
return new InjectionTransientLifetimeManager();
8586
default:

src/Lifetime/InjectionScopeLifetimeManager.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
using System;
2-
using System.Threading;
3-
using Unity.Exceptions;
42
using Unity.Lifetime;
53

6-
namespace Unity.Microsoft.DependencyInjection
4+
namespace Unity.Microsoft.DependencyInjection.Lifetime
75
{
8-
public class InjectionSingletonLifetimeManager : LifetimeManager, IRequiresRecovery
6+
public class InjectionSingletonLifetimeManager : ContainerControlledLifetimeManager
97
{
108
#region Fields
119

12-
protected object _value;
1310
private ILifetimeContainer _lifetime;
14-
private readonly object _lockObj = new object();
15-
11+
1612
#endregion
1713

1814

@@ -22,100 +18,36 @@ public InjectionSingletonLifetimeManager(ILifetimeContainer lifetime)
2218
}
2319

2420

25-
/// <summary>
26-
/// Retrieve a value from the backing store associated with this Lifetime policy.
27-
/// </summary>
28-
/// <returns>the object desired, or null if no such object is currently stored.</returns>
29-
/// <remarks>Calls to this method acquire a lock which is released only if a non-null value
30-
/// has been set for the lifetime manager.</remarks>
31-
public override object GetValue(ILifetimeContainer container = null)
21+
protected override void SynchronizedSetValue(object newValue, ILifetimeContainer container = null)
3222
{
33-
Monitor.Enter(_lockObj);
34-
var result = SynchronizedGetValue(container);
35-
if (result != null)
36-
{
37-
Monitor.Exit(_lockObj);
38-
}
39-
return result;
23+
base.SynchronizedSetValue(newValue, container);
24+
_lifetime.Add(new DisposableAction(() => RemoveValue(_lifetime)));
4025
}
4126

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

5432

55-
/// <summary>
56-
/// Stores the given value into backing store for retrieval later.
57-
/// </summary>
58-
/// <param name="newValue">The object being stored.</param>
59-
/// <param name="container">The container this value belongs to.</param>
60-
/// <remarks>Setting a value will attempt to release the lock acquired by
61-
/// <see cref="SynchronizedLifetimeManager.GetValue"/>.</remarks>
62-
public override void SetValue(object newValue, ILifetimeContainer container = null)
63-
{
64-
SynchronizedSetValue(newValue, container);
65-
TryExit();
66-
}
67-
68-
/// <summary>
69-
/// Performs the actual storage of the given value into backing store for retrieval later.
70-
/// </summary>
71-
/// <param name="newValue">The object being stored.</param>
72-
/// <param name="container"></param>
73-
/// <remarks>This method is invoked by <see cref="SynchronizedLifetimeManager.SetValue"/>
74-
/// before releasing its lock.</remarks>
75-
protected virtual void SynchronizedSetValue(object newValue, ILifetimeContainer container)
76-
{
77-
_value = newValue;
78-
if (_value is IDisposable disposable) _lifetime.Add(disposable);
79-
}
33+
#region Nested Types
8034

81-
/// <summary>
82-
/// A method that does whatever is needed to clean up
83-
/// as part of cleaning up after an exception.
84-
/// </summary>
85-
/// <remarks>
86-
/// Don't do anything that could throw in this method,
87-
/// it will cause later recover operations to get skipped
88-
/// and play real havoc with the stack trace.
89-
/// </remarks>
90-
public void Recover()
35+
private class DisposableAction : IDisposable
9136
{
92-
TryExit();
93-
}
37+
private readonly Action _action;
9438

95-
protected virtual void TryExit()
96-
{
97-
#if !NET40
98-
// Prevent first chance exception when abandoning a lock that has not been entered
99-
if (!Monitor.IsEntered(_lockObj)) return;
100-
#endif
101-
try
39+
public DisposableAction(Action action)
10240
{
103-
Monitor.Exit(_lockObj);
41+
_action = action ?? throw new ArgumentNullException(nameof(action));
10442
}
105-
catch (SynchronizationLockException)
43+
44+
public void Dispose()
10645
{
107-
// Noop here - we don't hold the lock and that's ok.
46+
_action();
10847
}
10948
}
11049

111-
public override void RemoveValue(ILifetimeContainer container = null)
112-
{
113-
TryExit();
114-
}
50+
#endregion
11551

116-
protected override LifetimeManager OnCreateLifetimeManager()
117-
{
118-
return new InjectionSingletonLifetimeManager(_lifetime);
119-
}
12052
}
12153
}

src/Lifetime/InjectionTransientLifetimeManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Unity.Lifetime;
33

4-
namespace Unity.Microsoft.DependencyInjection
4+
namespace Unity.Microsoft.DependencyInjection.Lifetime
55
{
66
/// <summary>
77
/// A special lifetime manager which works like <see cref="TransienLifetimeManager"/>,

src/Extension/MDIExtension.cs renamed to src/MDIExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using Unity.Extension;
22
using Unity.Lifetime;
3+
using Unity.Microsoft.DependencyInjection.Policy;
34
using Unity.Policy;
45

56
namespace Unity.Microsoft.DependencyInjection
67
{
7-
internal class MDIExtension : UnityContainerExtension
8+
internal class MdiExtension : UnityContainerExtension
89
{
910
protected override void Initialize()
1011
{

src/Policy/ConstructorSelectorPolicy.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5-
using Unity.Policy;
6-
using Unity.ObjectBuilder.BuildPlan.Selection;
5+
using Unity.Attributes;
76
using Unity.Builder;
87
using Unity.Builder.Selection;
9-
using Unity.Attributes;
8+
using Unity.ObjectBuilder.BuildPlan.Selection;
9+
using Unity.Policy;
1010
using Unity.ResolverPolicy;
1111

12-
namespace Unity.Microsoft.DependencyInjection
12+
namespace Unity.Microsoft.DependencyInjection.Policy
1313
{
1414
public class ConstructorSelectorPolicy : IConstructorSelectorPolicy
1515
{
16-
DefaultUnityConstructorSelectorPolicy dependency = new DefaultUnityConstructorSelectorPolicy();
16+
private readonly DefaultUnityConstructorSelectorPolicy _dependency = new DefaultUnityConstructorSelectorPolicy();
1717

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

3434
private ConstructorInfo FindDependencyConstructor<T>(IBuilderContext context)
@@ -57,6 +57,7 @@ private static ConstructorInfo FindSingleConstructor(IEnumerable<ConstructorInfo
5757
{
5858
if (constructors.Count() == 1)
5959
return constructors.First();
60+
6061
return null;
6162
}
6263

src/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/ServiceDescriptorExtensions.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)