|
9 | 9 | "source":"https://github.com/ItaiTzur76/GenerateDispose" |
10 | 10 | }, |
11 | 11 | "data":{ |
12 | | - "goodFor":["Generating the Dispose method for a class that implements IDisposable."], |
| 12 | + "goodFor":["GenerateDispose for boilerplate reduction for IDisposable pattern", |
| 13 | +"### Purpose", |
| 14 | +"A Roslyn source generator that replaces the 10+ lines of IDisposable boilerplate code with a single attribute.", |
| 15 | +"It also automatically adapts the generated pattern when the class modifiers change (e.g. sealed to non-sealed).", |
| 16 | +"", |
| 17 | +"### How to Define", |
| 18 | +"```csharp showLineNumbers", |
| 19 | +"[GenerateDispose.SourceGenerators.GenerateDispose(nameof(Drop))]", |
| 20 | +"partial class DALDB : IDisposable // : IDisposable is optional!", |
| 21 | +"{", |
| 22 | +" private ConnectionDB cn;", |
| 23 | +" private ConnectionDB cn1;", |
| 24 | +"", |
| 25 | +" public DALDB()", |
| 26 | +" {", |
| 27 | +" cn = new ConnectionDB();", |
| 28 | +" cn1 = new ConnectionDB();", |
| 29 | +" }", |
| 30 | +"", |
| 31 | +" public void Drop() // Your custom disposal logic", |
| 32 | +" {", |
| 33 | +" cn.Dispose();", |
| 34 | +" cn1.Dispose();", |
| 35 | +" }", |
| 36 | +"}", |
| 37 | +"```", |
| 38 | +"", |
| 39 | +"- The class must be partial", |
| 40 | +"- Pass nameof(YourDisposeMethod) to the attribute - the method must be callable with no arguments", |
| 41 | +"", |
| 42 | +"### What Gets Generated", |
| 43 | +"- public void Dispose() (thread-safe, calls your method)", |
| 44 | +"- A private int _isDisposed field for double-dispose protection", |
| 45 | +"- Adapts to sealed vs non-sealed automatically (private vs protected virtual)", |
| 46 | +"", |
| 47 | +"### How to Use", |
| 48 | +"```csharp showLineNumbers", |
| 49 | +"using (var db = new DALDB())", |
| 50 | +"{", |
| 51 | +" // use db...", |
| 52 | +"} // Dispose() called automatically", |
| 53 | +"", |
| 54 | +"```", |
| 55 | +"### Key Benefits", |
| 56 | +"- 10+ lines of boilerplate replaced by 1 attribute", |
| 57 | +"- sealed changes auto-adapt the Dispose pattern", |
| 58 | +"- No manual IDisposable wiring needed", |
| 59 | +""], |
13 | 60 | "csprojDemo":"IDisp.csproj", |
14 | 61 | "csFiles":["Program.cs","DALDB.cs","ConnectionDB.cs"], |
15 | 62 | "excludeDirectoryGenerated":[""], |
|
0 commit comments