1+ #region FreeBSD
2+
3+ // Copyright (c) 2013, John Batte
4+ // All rights reserved.
5+ //
6+ // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7+ //
8+ // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9+ //
10+ // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
11+ // documentation and/or other materials provided with the distribution.
12+ //
13+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
14+ // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
15+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
17+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
18+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
20+ #endregion
21+
22+ using System ;
23+
24+ namespace Patterns . Runtime
25+ {
26+ /// <summary>
27+ /// Provides a disposable scope with configurable setup and tear-down actions.
28+ /// </summary>
29+ public class TemporaryScope : IDisposable
30+ {
31+ private readonly Action _setup ;
32+ private readonly Action _tearDown ;
33+
34+ /// <summary>
35+ /// Initializes a new instance of the <see cref="TemporaryScope" /> class.
36+ /// </summary>
37+ /// <param name="setup">The setup.</param>
38+ /// <param name="tearDown">The tear down.</param>
39+ public TemporaryScope ( Action setup = null , Action tearDown = null )
40+ {
41+ _setup = setup ;
42+ _tearDown = tearDown ;
43+
44+ if ( _setup != null ) _setup ( ) ;
45+ }
46+
47+ /// <summary>
48+ /// Disposes this scope; if a tear-down method exists, it is executed.
49+ /// </summary>
50+ public void Dispose ( )
51+ {
52+ if ( Disposed ) throw new ObjectDisposedException ( GetType ( ) . Name ) ;
53+
54+ if ( _tearDown != null ) _tearDown ( ) ;
55+
56+ Disposed = true ;
57+ }
58+
59+ /// <summary>
60+ /// Gets or sets a value indicating whether this <see cref="TemporaryScope"/> is disposed.
61+ /// </summary>
62+ /// <value>
63+ /// <c>true</c> if disposed; otherwise, <c>false</c>.
64+ /// </value>
65+ public bool Disposed { get ; set ; }
66+ }
67+ }
0 commit comments