ProgrammerAl.SourceGenerators.PublicInterfaceGenerator
1.0.0.62
dotnet add package ProgrammerAl.SourceGenerators.PublicInterfaceGenerator --version 1.0.0.62
NuGet\Install-Package ProgrammerAl.SourceGenerators.PublicInterfaceGenerator -Version 1.0.0.62
<PackageReference Include="ProgrammerAl.SourceGenerators.PublicInterfaceGenerator" Version="1.0.0.62"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ProgrammerAl.SourceGenerators.PublicInterfaceGenerator --version 1.0.0.62
#r "nuget: ProgrammerAl.SourceGenerators.PublicInterfaceGenerator, 1.0.0.62"
// Install ProgrammerAl.SourceGenerators.PublicInterfaceGenerator as a Cake Addin #addin nuget:?package=ProgrammerAl.SourceGenerators.PublicInterfaceGenerator&version=1.0.0.62 // Install ProgrammerAl.SourceGenerators.PublicInterfaceGenerator as a Cake Tool #tool nuget:?package=ProgrammerAl.SourceGenerators.PublicInterfaceGenerator&version=1.0.0.62
interface-generator
The purpose of this project is to make a C# Source Generator to create Interfaces at compile time. The Source Generator will inspect all classes with the provided [GenerateInterfaceAttribute]
attribute and generate an Interface of all public Methods, Properties, and Events.
The default naming convention is to generate an interface of the same name as the class, with 'I' prepended to it. For example, MyClass
would generate an interface called IMyClass
.
The GenerateInterfaceAttribute
attribute has 2 properties.
InterfaceName
- This will override the default name for the interface that is generated. The name will be the string provided.Namespace
- By default, the same namespace as the class would be used for the generated interface. Setting thie property will override the default namespace and use the string provided.
NuGet Package
The NuGet package is hosted on nuget.org. You can get it from: https://www.nuget.org/packages/PublicInterfaceGenerator
Why does this exist?
Why spend my time on this and not something else? I'm glad you asked.
I don't know.
Seriously
Codifying code patterns can help keep code clean. You know it will always be a certain way, so why make a human write it out artisinally? That can be error prone, and also a waste of time. Make the machine do it.
A common pattern in C# applications is to create a class and an underlying interface with a similar name. A lot of the time this is done for unit testing purposes. The interface can be mocked in a unit test to simplify test setup. The interface always follows a pattern; look like the concrete implimentation, but an interface.
This means that this project will codify that pattern. A standard thing done in a project can have some code generated, so just have a tool generate it. Yes it saves a bit of time on manual work. But the real value is in knowing the pattern is always followed because the code is generated.
Sample Code
Imagine you have code like below. An interface, and concrete class, and a section of code where you register the interface/class with an IoC container.
public interface IUserManager
{
Task UpdateUserName(string name);
Task UpdateUserAddress(string address);
}
public class UserManager : IUserManager
{
public async Task UpdateUserName(string name)
{
await Task.Completed;
}
public async Task UpdateUserAddress(string address)
{
await Task.Completed;
}
}
//Register the interface to the .NET IoC Container
builder.Services.AddScoped<IUserManager, UserManager>();
When generating the interface, your code would look like this:
[GenerateInterface]
public class UserManager : IUserManager
{
public async Task UpdateUserName(string name)
{
await Task.Completed;
}
public async Task UpdateUserAddress(string address)
{
await Task.Completed;
}
}
//Register the interface to the .NET IoC Container
builder.Services.AddScoped<IUserManager, UserManager>();
Does that look cleaner? I'll leave that up to you. But that's what this project does.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0.62 | 389 | 9/30/2024 |
0.1.0-preview.65 | 47 | 10/16/2024 |
0.1.0-preview.61 | 83 | 9/29/2024 |
0.1.0-preview.59 | 102 | 9/27/2024 |
0.1.0-preview.57 | 69 | 9/27/2024 |
0.1.0-preview.55 | 53 | 9/27/2024 |
0.1.0-preview.53 | 53 | 9/27/2024 |
0.1.0-preview.45 | 59 | 9/26/2024 |
0.1.0-preview.42 | 58 | 9/26/2024 |
0.1.0-preview.39 | 47 | 9/25/2024 |
0.1.0-preview.36 | 60 | 9/25/2024 |
0.1.0-preview.34 | 45 | 9/25/2024 |
0.1.0-preview.32 | 49 | 9/25/2024 |