You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MakeInterface is a [C# source generator](https://learn.microsoft.com/dotnet/csharp/roslyn-sdk/source-generators-overview) that produces an `I{ClassName}` interface for any class marked with `GenerateInterface`. The generator analyses the public members of the class and writes the matching interface into your project's build output.
7
+
8
+
This is particularly useful when you simply need an interface to facilitate unit testing or dependency injection.
9
+
6
10
## Usage
7
-
Add the attribute to the class you want to generate the interface for
11
+
1. Install the NuGet package (see [Installation](#installation)).
12
+
2. Add the attribute to the class you want an interface for.
13
+
3. Build your project. The interface will appear in your `obj` folder and be part of the compilation.
8
14
```csharp
9
15
[GenerateInterface]
10
16
publicclassMyClass
11
17
{
12
-
publicstringMyProperty { get; set; }
13
-
publicvoidMyMethod() { }
18
+
publicstringMyProperty { get; set; }
19
+
publicvoidMyMethod() { }
20
+
}
21
+
```
22
+
23
+
Need to omit a member? Use the `Exclude` property to provide a list of member names:
@@ -27,15 +43,24 @@ You can then implement the interface in your class
27
43
```csharp
28
44
publicclassMyClass : IMyClass
29
45
{
30
-
publicstringMyProperty { get; set; }
31
-
publicvoidMyMethod() { }
46
+
publicstringMyProperty { get; set; }
47
+
publicvoidMyMethod() { }
32
48
}
33
49
```
34
50
51
+
## When should I generate interfaces?
52
+
Generating interfaces works well when you only need an interface so the class can be mocked in unit tests or injected into other components. In that scenario your class is typically the single implementation and keeping the interface in sync manually becomes boilerplate. Let the generator do the work for you.
53
+
54
+
If you maintain many implementations of the same interface or the interface needs to diverge from the class surface, consider writing the interface yourself. Manually created interfaces give you more control over its shape and versioning.
55
+
35
56
## Installation
36
-
Install the NuGet package [MakeInterface](https://www.nuget.org/packages/MakeInterface.Generator/)
57
+
Install the NuGet package [MakeInterface](https://www.nuget.org/packages/MakeInterface.Generator/):
58
+
59
+
```bash
60
+
dotnet add package MakeInterface.Generator
61
+
```
37
62
38
-
The required `GenerateInterface` attribute is automatically provided by the source generator, so no additional package reference is needed.
63
+
The `GenerateInterface` attribute is included in the package and will be available after the build without adding any extra references.
0 commit comments