From 5524f6b0bc17438b5a0273de191b850e79cdf469 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 25 Jul 2021 11:35:32 +0200 Subject: [PATCH] 0.0.1 --- README.md | 71 ++++++++++++++++++- .../ProxyInterfaceSourceGenerator.csproj | 32 +++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e9e008c..6e1e7e0 100644 --- a/README.md +++ b/README.md @@ -1 +1,70 @@ -# ProxyInterfaceSourceGenerator \ No newline at end of file +# ProxyInterfaceSourceGenerator + +This project uses Source Generation to generate an interface and a Proxy class for classes. +This makes it possible to wrap external classes which do not have an interface, in a Proxy class which makes it easier to Mock and use DI. + + +## Install +[![NuGet Badge](https://buildstats.info/nuget/ProxyInterfaceGenerator)](https://www.nuget.org/packages/ProxyInterfaceGenerator) + +You can install from NuGet using the following command in the package manager window: + +`Install-Package ProxyInterfaceGenerator` + +Or via the Visual Studio NuGet package manager or if you use the `dotnet` command: + +`dotnet add package ProxyInterfaceGenerator` + +## Usage + +### Given: an external existing class which does not implement an interface +``` c# +public sealed class Person +{ + public string Name { get; set; } +} +``` + +## Create a partial interface +And annotate this with `ProxyInterfaceGenerator.Proxy` with the Type which needs to be wrapped: + +``` c# +[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Person))] +public partial interface IPerson +{ +} +``` + +## When the code is compiled, this source generator creates the following + +### 1. An additional partial interface +Which defines the same properties and methods as in the external class. +``` c# +public partial interface IPerson +{ + string Name { get; set; } +} +``` + +### 2. A Proxy class +Which takes the external class in the constructor and wraps all properties. + +``` c# +public class PersonProxy +{ + public Person _Instance { get; } + + public PersonProxy(Person instance) + { + _Instance = instance; + } + + public string Name { get => _Instance.Name; set => _Instance.Name = value; } +} +``` + +## Use it +``` c# +IPerson p = new PersonProxy(new Person()); +p.Name = "test"; +``` \ No newline at end of file diff --git a/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj b/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj index baa9718..fb084f3 100644 --- a/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj +++ b/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj @@ -5,6 +5,29 @@ netstandard2.0 9 enable + Stef Heyenrath + + ProxyInterfaceGenerator + ProxyInterfaceGenerator + This project uses Source Generation to generate an interface and a Proxy class for classes. This makes it possible to wrap external classes which do not have an interface, in a Proxy class which makes it easier to Mock and use DI. + class;Fluent;Builder;FluentBuilder;Model;DTO;SourceGenerator;Analyzer;Generation;Generate + + MIT + See ReleaseNotes.md + https://github.com/StefH/ProxyInterfaceGenerator + git + https://github.com/StefH/ProxyInterfaceGenerator + + netstandard2.0 + {12344228-91F4-4502-9595-39584E5ABB34} + + false + true + $(BaseIntermediateOutputPath)Generated + + + + true @@ -16,4 +39,13 @@ + + + + + + + + + \ No newline at end of file