Skip to content

Commit c6deef9

Browse files
authored
Merge pull request #46 from Frederik91/codex/improve-readme-for-package-usage
Improve readme usage documentation
2 parents 5e6cf2c + fc3ef90 commit c6deef9

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

readme.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
# MakeInterface
2-
Creates an interface of a class using source generator
2+
Generate interfaces for your classes at compile time using a simple attribute.
33

44
[![.NET](https://github.com/Frederik91/MakeInterface/actions/workflows/dotnet.yml/badge.svg)](https://github.com/Frederik91/MakeInterface/actions/workflows/dotnet.yml)
55

6+
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+
610
## 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.
814
```csharp
915
[GenerateInterface]
1016
public class MyClass
1117
{
12-
public string MyProperty { get; set; }
13-
public void MyMethod() { }
18+
public string MyProperty { get; set; }
19+
public void MyMethod() { }
20+
}
21+
```
22+
23+
Need to omit a member? Use the `Exclude` property to provide a list of member names:
24+
```csharp
25+
[GenerateInterface(Exclude = new[] { nameof(MyMethod) })]
26+
public class MyClass
27+
{
28+
public string MyProperty { get; set; }
29+
public void MyMethod() { }
1430
}
1531
```
1632

@@ -27,15 +43,24 @@ You can then implement the interface in your class
2743
```csharp
2844
public class MyClass : IMyClass
2945
{
30-
public string MyProperty { get; set; }
31-
public void MyMethod() { }
46+
public string MyProperty { get; set; }
47+
public void MyMethod() { }
3248
}
3349
```
3450

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+
3556
## 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+
```
3762

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.
3964

4065

4166
## License

0 commit comments

Comments
 (0)