-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGenerateDispose.html
More file actions
75 lines (56 loc) · 3.85 KB
/
Copy pathGenerateDispose.html
File metadata and controls
75 lines (56 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<h1>RSCG nr 268 : GenerateDispose</h1>
<h2>Info</h2>
Nuget : <a href="https://www.nuget.org/packages/GenerateDispose/" target="_blank">https://www.nuget.org/packages/GenerateDispose/</a>
<p>You can find more details at : <a href="https://github.com/ItaiTzur76/GenerateDispose" target="_blank"> https://github.com/ItaiTzur76/GenerateDispose</a></p>
<p>Author :Itai Tzur</p>
<p>Source: <a href="https://github.com/ItaiTzur76/GenerateDispose" target="_blank">https://github.com/ItaiTzur76/GenerateDispose</a> </p>
<h2>About</h2>
GenerateDispose for boilerplate reduction for IDisposable pattern### PurposeA Roslyn source generator that replaces the 10+ lines of IDisposable boilerplate code with a single attribute.It also automatically adapts the generated pattern when the class modifiers change (e.g. sealed to non-sealed).### How to Define```csharp showLineNumbers[GenerateDispose.SourceGenerators.GenerateDispose(nameof(Drop))]partial class DALDB : IDisposable // : IDisposable is optional!{ private ConnectionDB cn; private ConnectionDB cn1; public DALDB() { cn = new ConnectionDB(); cn1 = new ConnectionDB(); } public void Drop() // Your custom disposal logic { cn.Dispose(); cn1.Dispose(); }}```- The class must be partial- Pass nameof(YourDisposeMethod) to the attribute - the method must be callable with no arguments### What Gets Generated- public void Dispose() (thread-safe, calls your method)- A private int _isDisposed field for double-dispose protection- Adapts to sealed vs non-sealed automatically (private vs protected virtual)### How to Use```csharp showLineNumbersusing (var db = new DALDB()){ // use db...} // Dispose() called automatically```### Key Benefits- 10+ lines of boilerplate replaced by 1 attribute- sealed changes auto-adapt the Dispose pattern- No manual IDisposable wiring needed
<h2>
How to use
</h2>
<h3>
Add reference to the <a href="https://www.nuget.org/packages/GenerateDispose/" target="_blank">GenerateDispose</a> in the csproj
</h3>
<img src="images/GenerateDispose/IDisp.csproj.png" width="580" height="580" />
<h3>This was for me the <b>starting</b> code</h3>
<br />
I have <b>coded</b> the file Program.cs
<br />
<img src="images/GenerateDispose/csFiles/Program.cs.png" width="580" height="580" />
<hr />
<br />
I have <b>coded</b> the file DALDB.cs
<br />
<img src="images/GenerateDispose/csFiles/DALDB.cs.png" width="580" height="580" />
<hr />
<br />
I have <b>coded</b> the file ConnectionDB.cs
<br />
<img src="images/GenerateDispose/csFiles/ConnectionDB.cs.png" width="580" height="580" />
<hr />
<h3>And here are the <i>generated</i> files</h3>
<br />
The file <i>generated</i> is GenerateDisposeAttribute.g.cs
<br />
<img src="images/GenerateDispose/generated/GenerateDisposeAttribute.g.cs.png" width="580" height="580" />
<br />
The file <i>generated</i> is IDisposableGeneratorDemo.DALDB.g.cs
<br />
<img src="images/GenerateDispose/generated/IDisposableGeneratorDemo.DALDB.g.cs.png" width="580" height="580" />
<br />
The file <i>generated</i> is Microsoft.CodeAnalysis.EmbeddedAttribute.cs
<br />
<img src="images/GenerateDispose/generated/Microsoft.CodeAnalysis.EmbeddedAttribute.cs.png" width="580" height="580" />
<p>
You can download the code and this page as pdf from
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/GenerateDispose'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/GenerateDispose
</a>
</p>
<p>
You can see the whole list at
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG
</a>
</p>