11using System ;
22using System . Threading ;
3+ using System . Threading . Tasks ;
34
45namespace 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