1- //
2- // Copyright(C) 2017-2021, Daniel M. Porrey. All rights reserved.
3- //
4- // This program is free software: you can redistribute it and/or modify
5- // it under the terms of the GNU Lesser General Public License as published
6- // by the Free Software Foundation, either version 3 of the License, or
7- // (at your option) any later version.
8- //
9- // This program is distributed in the hope that it will be useful,
10- // but WITHOUT ANY WARRANTY; without even the implied warranty of
11- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12- // GNU Lesser General Public License for more details.
13- //
14- // You should have received a copy of the GNU Lesser General Public License
15- // along with this program. If not, see http://www.gnu.org/licenses/.
16- //
17- using System . Diagnostics ;
18- using System . Threading . Tasks ;
19-
20- namespace System
21- {
22- /// <summary>
23- /// This class provides base functionality for implementing
24- /// <see cref="IAsyncDisposable"/>. Since we are dealing with
25- /// database resources, disposing of objects is important. Any
26- /// class that inherits from this class simply needs to override
27- /// OnDisposeManagedObjects and/or OnDisposeUnmanagedObjects.
28- /// </summary>
29- public abstract class AsyncDisposableObject : DisposableObject , IAsyncDisposable
30- {
31- /// <summary>
32- /// Default constructor for <see cref="AsyncDisposableObject"/>.
33- /// </summary>
34- public AsyncDisposableObject ( )
35- {
36- //
37- // Set this to True for debugging.
38- //
39- this . AssertWhenNotDisposed = false ;
40- }
41-
42- /// <summary>
43- /// Default destructor for <see cref="AsyncDisposableObject"/>.
44- /// </summary>
45- ~ AsyncDisposableObject ( )
46- {
47- //
48- // Write a trace (to the debugger) showing this method was called (it will only
49- // get called if this object is not Disposed).
50- //
51- Trace . TraceWarning ( "~BaseObject called on {0}" , this . OnGetClassName ( ) ) ;
52-
53- //
54- // Give the parent object a chance to respond, if not then this
55- // class will assert.
56- //
57- if ( this . AssertWhenNotDisposed )
58- {
59- if ( ! this . OnNotDisposedProperly ( ) )
60- {
61- //
62- // Assert if this object is destroyed without being disposed. Even though
63- // dispose is called here, it is more ideal that it be called by the user
64- // of the object. This assert will help catch this instance.
65- //
66- Trace . Assert ( this . IsDisposed , this . OnGetClassName ( ) + " was not disposed properly." ) ;
67- }
68- else
69- {
70- Trace . TraceWarning ( "{0} was not disposed properly." , this . OnGetClassName ( ) ) ;
71- }
72- }
73-
74- //
75- // This destructor is only called by garbage collection. Because of this, this object
76- // can no longer access managed objects. Only unmanaged objects will be cleaned up
77- // here.
78- //
79- this . Dispose ( false ) ;
80- }
81-
82- /// <summary>
83- /// Performs application-defined tasks associated with freeing, releasing, or resetting
84- /// unmanaged resources asynchronously.
85- /// </summary>
86- /// <returns></returns>
87- public virtual ValueTask DisposeAsync ( )
88- {
89- this . Dispose ( ) ;
90- return ValueTask . CompletedTask ;
91- }
92- }
1+ //
2+ // Copyright(C) 2017-2021, Daniel M. Porrey. All rights reserved.
3+ //
4+ // This program is free software: you can redistribute it and/or modify
5+ // it under the terms of the GNU Lesser General Public License as published
6+ // by the Free Software Foundation, either version 3 of the License, or
7+ // (at your option) any later version.
8+ //
9+ // This program is distributed in the hope that it will be useful,
10+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ // GNU Lesser General Public License for more details.
13+ //
14+ // You should have received a copy of the GNU Lesser General Public License
15+ // along with this program. If not, see http://www.gnu.org/licenses/.
16+ //
17+ using System . Diagnostics ;
18+ using System . Threading . Tasks ;
19+
20+ namespace System
21+ {
22+ /// <summary>
23+ /// This class provides base functionality for implementing
24+ /// <see cref="IAsyncDisposable"/>. Since we are dealing with
25+ /// database resources, disposing of objects is important. Any
26+ /// class that inherits from this class simply needs to override
27+ /// OnDisposeManagedObjects and/or OnDisposeUnmanagedObjects.
28+ /// </summary>
29+ public abstract class AsyncDisposableObject : DisposableObject , IAsyncDisposable
30+ {
31+ /// <summary>
32+ /// Default constructor for <see cref="AsyncDisposableObject"/>.
33+ /// </summary>
34+ public AsyncDisposableObject ( )
35+ {
36+ //
37+ // Set this to True for debugging.
38+ //
39+ this . AssertWhenNotDisposed = false ;
40+ }
41+
42+ /// <summary>
43+ /// Default destructor for <see cref="AsyncDisposableObject"/>.
44+ /// </summary>
45+ ~ AsyncDisposableObject ( )
46+ {
47+ //
48+ // Write a trace (to the debugger) showing this method was called (it will only
49+ // get called if this object is not Disposed).
50+ //
51+ Trace . TraceWarning ( "~BaseObject called on {0}" , this . OnGetClassName ( ) ) ;
52+
53+ //
54+ // Give the parent object a chance to respond, if not then this
55+ // class will assert.
56+ //
57+ if ( this . AssertWhenNotDisposed )
58+ {
59+ if ( ! this . OnNotDisposedProperly ( ) )
60+ {
61+ //
62+ // Assert if this object is destroyed without being disposed. Even though
63+ // dispose is called here, it is more ideal that it be called by the user
64+ // of the object. This assert will help catch this instance.
65+ //
66+ Trace . Assert ( this . IsDisposed , this . OnGetClassName ( ) + " was not disposed properly." ) ;
67+ }
68+ else
69+ {
70+ Trace . TraceWarning ( "{0} was not disposed properly." , this . OnGetClassName ( ) ) ;
71+ }
72+ }
73+
74+ //
75+ // This destructor is only called by garbage collection. Because of this, this object
76+ // can no longer access managed objects. Only unmanaged objects will be cleaned up
77+ // here.
78+ //
79+ this . Dispose ( false ) ;
80+ }
81+
82+ /// <summary>
83+ /// Performs application-defined tasks associated with freeing, releasing, or resetting
84+ /// unmanaged resources asynchronously.
85+ /// </summary>
86+ /// <returns></returns>
87+ public virtual ValueTask DisposeAsync ( )
88+ {
89+ this . Dispose ( ) ;
90+ return ValueTask . CompletedTask ;
91+ }
92+ }
9393}
0 commit comments