Skip to content

Commit a149695

Browse files
committed
Converted to use ValueTask<TResult>
1 parent dfbe05c commit a149695

13 files changed

+164
-163
lines changed

src/Abstracts/IUnityContainerAsync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public interface IUnityContainerAsync : IDisposable
9595
/// <param name="name">Name of the object to retrieve.</param>
9696
/// <param name="overrides">Any overrides for the resolve call.</param>
9797
/// <returns>The retrieved object.</returns>
98-
Task<object> Resolve(Type type, string name, params ResolverOverride[] overrides);
98+
ValueTask<object> Resolve(Type type, string name, params ResolverOverride[] overrides);
9999

100100

101101
/// <summary>
@@ -106,7 +106,7 @@ public interface IUnityContainerAsync : IDisposable
106106
/// <see cref="Regex.IsMatch(string name)"/> will be resolved</param>
107107
/// <param name="overrides">Any overrides for the resolve call.</param>
108108
/// <returns>The retrieved object.</returns>
109-
Task<IEnumerable<object>> Resolve(Type type, Regex regex, params ResolverOverride[] overrides);
109+
ValueTask<IEnumerable<object>> Resolve(Type type, Regex regex, params ResolverOverride[] overrides);
110110

111111

112112
/// <summary>

src/Extensions/Container/UnityContainerAsync.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,10 @@ public static IUnityContainerAsync RegisterFactory(this IUnityContainerAsync con
879879
#if !NET40
880880
[MethodImpl(MethodImplOptions.AggressiveInlining)]
881881
#endif
882-
public static Task<object> Resolve<T>(this IUnityContainerAsync container, params ResolverOverride[] overrides)
882+
public static ValueTask<object> Resolve<T>(this IUnityContainerAsync container, params ResolverOverride[] overrides)
883883
{
884-
return (container ?? throw new ArgumentNullException(nameof(container))).Resolve(typeof(T), overrides);
884+
return (container ?? throw new ArgumentNullException(nameof(container)))
885+
.Resolve(typeof(T), (string)null, overrides);
885886
}
886887

887888
/// <summary>
@@ -895,9 +896,10 @@ public static Task<object> Resolve<T>(this IUnityContainerAsync container, param
895896
#if !NET40
896897
[MethodImpl(MethodImplOptions.AggressiveInlining)]
897898
#endif
898-
public static Task<object> Resolve<T>(this IUnityContainerAsync container, string name, params ResolverOverride[] overrides)
899+
public static ValueTask<object> Resolve<T>(this IUnityContainerAsync container, string name, params ResolverOverride[] overrides)
899900
{
900-
return (container ?? throw new ArgumentNullException(nameof(container))).Resolve(typeof(T), name, overrides);
901+
return (container ?? throw new ArgumentNullException(nameof(container)))
902+
.Resolve(typeof(T), name, overrides);
901903
}
902904

903905
/// <summary>
@@ -910,7 +912,7 @@ public static Task<object> Resolve<T>(this IUnityContainerAsync container, strin
910912
#if !NET40
911913
[MethodImpl(MethodImplOptions.AggressiveInlining)]
912914
#endif
913-
public static Task<object> Resolve(this IUnityContainerAsync container, Type t, params ResolverOverride[] overrides)
915+
public static ValueTask<object> Resolve(this IUnityContainerAsync container, Type t, params ResolverOverride[] overrides)
914916
{
915917
return (container ?? throw new ArgumentNullException(nameof(container))).Resolve(t, (string)null, overrides);
916918
}

src/Extensions/Lifetime/FactoryLifetime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public static class FactoryLifetime
165165
/// create and return a new instance.</para>
166166
/// <para>This lifetime manager does not dispose an object when container is disposed</para>
167167
/// </remarks>
168-
/// <value>A new instance of a <see cref="WeakReferenceLifetimeManager"/> lifetime manager.</value>
169-
public static IFactoryLifetimeManager WeakReference => new WeakReferenceLifetimeManager();
168+
/// <value>A new instance of a <see cref="ExternallyControlledLifetimeManager"/> lifetime manager.</value>
169+
public static IFactoryLifetimeManager External => new ExternallyControlledLifetimeManager();
170170
}
171171
}

src/Extensions/Lifetime/InstanceLifetime.cs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ namespace Unity
55
public static class InstanceLifetime
66
{
77
/// <summary>
8-
/// This lifetime stores a strong reference to an instance.
8+
/// This lifetime stores a weak reference to an instance and returns an instance while it is available.
99
/// </summary>
1010
/// <remarks>
11-
/// <para>Container holds reference to the object and keeps it alive as long as the container
12-
/// exists.</para>
13-
/// <para>If the object went out of scope and has been disposed elsewhere the container will
14-
/// still hold on to it and return is as requested.</para>
11+
/// <para>Container gets a hold of a weak reference to the object and as long as the object
12+
/// still exists and has not been garbage collected the container will return the object when
13+
/// requested.</para>
14+
/// <para>If the object went out of scope and has been garbage collected the container will
15+
/// return <see cref="LifetimeManager.NoValue"/>.</para>
1516
/// <para>This lifetime manager does not dispose an object when container is disposed</para>
1617
/// </remarks>
1718
/// <value>A new instance of a <see cref="ExternallyControlledLifetimeManager"/> lifetime manager.</value>
@@ -45,19 +46,5 @@ public static class InstanceLifetime
4546
/// </remarks>
4647
/// <value>A new instance of a <see cref="ContainerControlledLifetimeManager"/> object.</value>
4748
public static IInstanceLifetimeManager PerContainer => new ContainerControlledLifetimeManager();
48-
49-
/// <summary>
50-
/// This lifetime stores a weak reference to an instance and returns an instance while it is available.
51-
/// </summary>
52-
/// <remarks>
53-
/// <para>Container gets a hold of a weak reference to the object and as long as the object
54-
/// still exists and has not been garbage collected the container will return the object when
55-
/// requested.</para>
56-
/// <para>If the object went out of scope and has been garbage collected the container will
57-
/// return <see cref="LifetimeManager.NoValue"/>.</para>
58-
/// <para>This lifetime manager does not dispose an object when container is disposed</para>
59-
/// </remarks>
60-
/// <value>A new instance of a <see cref="WeakReferenceLifetimeManager"/> lifetime manager.</value>
61-
public static IFactoryLifetimeManager WeakReference => new WeakReferenceLifetimeManager();
6249
}
6350
}

src/Extensions/Lifetime/TypeLifetime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,6 @@ public static class TypeLifetime
180180
/// <para>This lifetime manager does not dispose an object when container is disposed</para>
181181
/// </remarks>
182182
/// <value>A new instance of a <see cref="WeakReferenceLifetimeManager"/> lifetime manager.</value>
183-
public static IFactoryLifetimeManager WeakReference => new WeakReferenceLifetimeManager();
183+
public static ITypeLifetimeManager External => new ExternallyControlledLifetimeManager();
184184
}
185185
}

src/Lifetime/Abstracts/ILifetimeManagerAsync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ namespace Unity.Lifetime
66
/// <summary>
77
/// Base for all async lifetime managers.
88
/// </summary>
9-
public interface ILifetimeManagerAsync : IDisposable
9+
public interface ILifetimeManagerAsync
1010
{
1111
Func<ILifetimeContainer, object> GetResult { get; }
1212

13-
Func<object, ILifetimeContainer, object> SetResult { get; }
13+
Action<object, ILifetimeContainer> SetResult { get; }
1414

1515
Func<ILifetimeContainer, Task<object>> GetTask { get; }
1616

src/Lifetime/Abstracts/LifetimeManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Unity.Lifetime
88
/// </summary>
99
public abstract class LifetimeManager
1010
{
11+
protected object SyncRoot { get; } = new object();
12+
1113
/// <summary>
1214
/// This value represents Invalid Value. Lifetime manager must return this
1315
/// unless value is set with a valid object. Null is a value and is not equal

src/Lifetime/Abstracts/SynchronizedLifetimeManager.cs

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading;
3+
using System.Threading.Tasks;
34

45
namespace Unity.Lifetime
56
{
@@ -24,8 +25,6 @@ public abstract class SynchronizedLifetimeManager : LifetimeManager, IDisposable
2425
{
2526
#region Fields
2627

27-
private readonly object _lock = new object();
28-
2928
/// <summary>
3029
/// This field controls how long the monitor will wait to
3130
/// enter the lock. It is <see cref="Timeout.Infinite"/> by default or number of
@@ -35,22 +34,64 @@ public abstract class SynchronizedLifetimeManager : LifetimeManager, IDisposable
3534

3635
#endregion
3736

37+
38+
#region Constructors
39+
40+
public SynchronizedLifetimeManager()
41+
{
42+
GetResult = GetValue;
43+
SetResult = SetValue;
44+
45+
GetTask = (c) => throw new NotImplementedException();
46+
SetTask = (t, c) => throw new NotImplementedException();
47+
}
48+
49+
#endregion
50+
51+
52+
#region ILifetimeManagerAsync
53+
54+
public Func<ILifetimeContainer, object> GetResult { get; protected set; }
55+
56+
public Action<object, ILifetimeContainer> SetResult { get; protected set; }
57+
58+
public Func<ILifetimeContainer, Task<object>> GetTask { get; protected set; }
59+
60+
public Func<Task<object>, ILifetimeContainer, Task<object>> SetTask { get; protected set; }
61+
62+
#endregion
63+
64+
65+
#region LifetimeManager
66+
3867
/// <inheritdoc/>
3968
public override object GetValue(ILifetimeContainer container = null)
4069
{
41-
if (Monitor.TryEnter(_lock, ResolveTimeout))
70+
if (Monitor.TryEnter(SyncRoot, ResolveTimeout))
4271
{
4372
var result = SynchronizedGetValue(container);
4473
if (NoValue != result)
4574
{
46-
Monitor.Exit(_lock);
75+
Monitor.Exit(SyncRoot);
4776
}
4877
return result;
4978
}
5079
else
5180
throw new TimeoutException($"Failed to enter a monitor");
5281
}
5382

83+
/// <inheritdoc/>
84+
public override void SetValue(object newValue, ILifetimeContainer container = null)
85+
{
86+
SynchronizedSetValue(newValue, container);
87+
TryExit();
88+
}
89+
90+
#endregion
91+
92+
93+
#region SynchronizedLifetimeManager
94+
5495
/// <summary>
5596
/// Performs the actual retrieval of a value from the backing store associated
5697
/// with this Lifetime policy.
@@ -61,14 +102,6 @@ public override object GetValue(ILifetimeContainer container = null)
61102
/// <returns>the object desired, or null if no such object is currently stored.</returns>
62103
protected abstract object SynchronizedGetValue(ILifetimeContainer container);
63104

64-
65-
/// <inheritdoc/>
66-
public override void SetValue(object newValue, ILifetimeContainer container = null)
67-
{
68-
SynchronizedSetValue(newValue, container);
69-
TryExit();
70-
}
71-
72105
/// <summary>
73106
/// Performs the actual storage of the given value into backing store for retrieval later.
74107
/// </summary>
@@ -92,34 +125,41 @@ public void Recover()
92125
TryExit();
93126
}
94127

128+
#endregion
129+
130+
131+
#region IDisposable
132+
133+
/// <summary>
134+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
135+
/// </summary>
136+
public void Dispose()
137+
{
138+
Dispose(true);
139+
GC.SuppressFinalize(this);
140+
}
141+
142+
#endregion
143+
144+
145+
#region Implementation
146+
95147
protected virtual void TryExit()
96148
{
97149
#if !NET40
98150
// Prevent first chance exception when abandoning a lock that has not been entered
99-
if (!Monitor.IsEntered(_lock)) return;
151+
if (!Monitor.IsEntered(SyncRoot)) return;
100152
#endif
101153
try
102154
{
103-
Monitor.Exit(_lock);
155+
Monitor.Exit(SyncRoot);
104156
}
105157
catch (SynchronizationLockException)
106158
{
107159
// Ignore.
108160
}
109161
}
110162

111-
112-
#region IDisposable
113-
114-
/// <summary>
115-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
116-
/// </summary>
117-
public void Dispose()
118-
{
119-
Dispose(true);
120-
GC.SuppressFinalize(this);
121-
}
122-
123163
/// <summary>
124164
/// Standard Dispose pattern implementation.
125165
/// </summary>

0 commit comments

Comments
 (0)