diff --git a/ProxyInterfaceSourceGenerator Solution.sln b/ProxyInterfaceSourceGenerator Solution.sln index 3967147..f915b17 100644 --- a/ProxyInterfaceSourceGenerator Solution.sln +++ b/ProxyInterfaceSourceGenerator Solution.sln @@ -16,31 +16,38 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{19009F5B EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src-examples", "src-examples", "{38BA087F-EDA1-4F8A-A140-85B84791B815}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceSourceGenerator", "src\ProxyInterfaceSourceGenerator\ProxyInterfaceSourceGenerator.csproj", "{C880D4B0-1E9E-4449-AEF8-0A93FB2EDFAD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceSourceGenerator", "src\ProxyInterfaceSourceGenerator\ProxyInterfaceSourceGenerator.csproj", "{12344228-91F4-4502-9595-39584E5ABB34}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceConsumer", "src-examples\ProxyInterfaceConsumer\ProxyInterfaceConsumer.csproj", "{7E0A10EE-CCC3-4281-9541-B0AF037D3DF9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyInterfaceConsumerViaNuGet", "src-examples\ProxyInterfaceConsumerViaNuGet\ProxyInterfaceConsumerViaNuGet.csproj", "{6BEBFEB9-635F-44A2-949C-15DDDF0B7740}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C880D4B0-1E9E-4449-AEF8-0A93FB2EDFAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C880D4B0-1E9E-4449-AEF8-0A93FB2EDFAD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C880D4B0-1E9E-4449-AEF8-0A93FB2EDFAD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C880D4B0-1E9E-4449-AEF8-0A93FB2EDFAD}.Release|Any CPU.Build.0 = Release|Any CPU + {12344228-91F4-4502-9595-39584E5ABB34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {12344228-91F4-4502-9595-39584E5ABB34}.Debug|Any CPU.Build.0 = Debug|Any CPU + {12344228-91F4-4502-9595-39584E5ABB34}.Release|Any CPU.ActiveCfg = Release|Any CPU + {12344228-91F4-4502-9595-39584E5ABB34}.Release|Any CPU.Build.0 = Release|Any CPU {7E0A10EE-CCC3-4281-9541-B0AF037D3DF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {7E0A10EE-CCC3-4281-9541-B0AF037D3DF9}.Debug|Any CPU.Build.0 = Debug|Any CPU {7E0A10EE-CCC3-4281-9541-B0AF037D3DF9}.Release|Any CPU.ActiveCfg = Release|Any CPU {7E0A10EE-CCC3-4281-9541-B0AF037D3DF9}.Release|Any CPU.Build.0 = Release|Any CPU + {6BEBFEB9-635F-44A2-949C-15DDDF0B7740}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6BEBFEB9-635F-44A2-949C-15DDDF0B7740}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6BEBFEB9-635F-44A2-949C-15DDDF0B7740}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6BEBFEB9-635F-44A2-949C-15DDDF0B7740}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {C880D4B0-1E9E-4449-AEF8-0A93FB2EDFAD} = {ED3DA9DD-1E07-444B-A2D7-2DBA280F96D4} + {12344228-91F4-4502-9595-39584E5ABB34} = {ED3DA9DD-1E07-444B-A2D7-2DBA280F96D4} {7E0A10EE-CCC3-4281-9541-B0AF037D3DF9} = {38BA087F-EDA1-4F8A-A140-85B84791B815} + {6BEBFEB9-635F-44A2-949C-15DDDF0B7740} = {38BA087F-EDA1-4F8A-A140-85B84791B815} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {585F071D-051D-441C-9C6B-226D9E15A1F5} diff --git a/README.md b/README.md index 6e1e7e0..a8baa25 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ -# ProxyInterfaceSourceGenerator +# 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. - ## Install [![NuGet Badge](https://buildstats.info/nuget/ProxyInterfaceGenerator)](https://www.nuget.org/packages/ProxyInterfaceGenerator) @@ -21,12 +20,12 @@ Or via the Visual Studio NuGet package manager or if you use the `dotnet` comman ``` c# public sealed class Person { - public string Name { get; set; } + public string Name { get; set; } } ``` -## Create a partial interface -And annotate this with `ProxyInterfaceGenerator.Proxy` with the Type which needs to be wrapped: +### Create a partial interface +And annotate this with `ProxyInterfaceGenerator.Proxy[...]` and with the Type which needs to be wrapped: ``` c# [ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Person))] @@ -35,24 +34,24 @@ public partial interface IPerson } ``` -## When the code is compiled, this source generator creates the following +### When the code is compiled, this source generator creates the following -### 1. An additional partial interface +#### :one: 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; } + string Name { get; set; } } ``` -### 2. A Proxy class +#### :two: A Proxy class Which takes the external class in the constructor and wraps all properties. ``` c# -public class PersonProxy +public class PersonProxy : IPerson { - public Person _Instance { get; } + public Person _Instance { get; } public PersonProxy(Person instance) { @@ -63,8 +62,8 @@ public class PersonProxy } ``` -## Use it +### Use it ``` c# IPerson p = new PersonProxy(new Person()); p.Name = "test"; -``` \ No newline at end of file +``` diff --git a/src-examples/ProxyInterfaceConsumer/IMyInterface.cs b/src-examples/ProxyInterfaceConsumer/IMyInterface.cs new file mode 100644 index 0000000..d616e32 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumer/IMyInterface.cs @@ -0,0 +1,6 @@ +namespace ProxyInterfaceConsumer +{ + public interface IMyInterface + { + } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumer/Person.cs b/src-examples/ProxyInterfaceConsumer/Person.cs index bfdb3f6..fba8ddf 100644 --- a/src-examples/ProxyInterfaceConsumer/Person.cs +++ b/src-examples/ProxyInterfaceConsumer/Person.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using Microsoft.CodeAnalysis; namespace ProxyInterfaceConsumer { @@ -20,6 +19,8 @@ namespace ProxyInterfaceConsumer public E E { get; set; } + public IMyInterface MyInterface { get; set; } + public int Add(string s) { return 600; @@ -32,12 +33,7 @@ namespace ProxyInterfaceConsumer public void Void() { - // } - - public Compilation Compilation { get; set; } - - public INamedTypeSymbol MyNamedTypeSymbol { get; set; } } public enum E diff --git a/src-examples/ProxyInterfaceConsumer/Program.cs b/src-examples/ProxyInterfaceConsumer/Program.cs index b05a868..7d5ec4e 100644 --- a/src-examples/ProxyInterfaceConsumer/Program.cs +++ b/src-examples/ProxyInterfaceConsumer/Program.cs @@ -1,11 +1,17 @@ using System; using System.Collections.Generic; +using System.Text.Json; using AutoMapper; namespace ProxyInterfaceConsumer { public class Program { + private static JsonSerializerOptions JsonSerializerOptions = new () + { + WriteIndented = true + }; + public static void Main() { var c = new Clazz @@ -20,7 +26,7 @@ namespace ProxyInterfaceConsumer var tp = new TestProxy(t); tp.Cs = new List { cp }; - Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(t)); + Console.WriteLine(JsonSerializer.Serialize(t, JsonSerializerOptions)); Console.WriteLine(new string('-', 80)); @@ -35,7 +41,7 @@ namespace ProxyInterfaceConsumer //p.Compilation = null; //p.Add("x"); //p.Void(); - Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(p)); + Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions)); } } diff --git a/src-examples/ProxyInterfaceConsumer/ProxyInterfaceConsumer.csproj b/src-examples/ProxyInterfaceConsumer/ProxyInterfaceConsumer.csproj index 46d5224..5bb31b9 100644 --- a/src-examples/ProxyInterfaceConsumer/ProxyInterfaceConsumer.csproj +++ b/src-examples/ProxyInterfaceConsumer/ProxyInterfaceConsumer.csproj @@ -7,13 +7,12 @@ - + + --> diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/Address.cs b/src-examples/ProxyInterfaceConsumerViaNuGet/Address.cs new file mode 100644 index 0000000..c4db154 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/Address.cs @@ -0,0 +1,7 @@ +namespace ProxyInterfaceConsumer +{ + public class Address + { + public int HouseNumber { get; set; } + } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/IAddress.cs b/src-examples/ProxyInterfaceConsumerViaNuGet/IAddress.cs new file mode 100644 index 0000000..514c7fd --- /dev/null +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/IAddress.cs @@ -0,0 +1,7 @@ +namespace ProxyInterfaceConsumer +{ + [ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Address))] + public partial interface IAddress + { + } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/IMyInterface.cs b/src-examples/ProxyInterfaceConsumerViaNuGet/IMyInterface.cs new file mode 100644 index 0000000..d616e32 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/IMyInterface.cs @@ -0,0 +1,6 @@ +namespace ProxyInterfaceConsumer +{ + public interface IMyInterface + { + } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/IPerson.cs b/src-examples/ProxyInterfaceConsumerViaNuGet/IPerson.cs new file mode 100644 index 0000000..e565cb5 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/IPerson.cs @@ -0,0 +1,7 @@ +namespace ProxyInterfaceConsumer +{ + [ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Person))] + public partial interface IPerson + { + } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/Person.cs b/src-examples/ProxyInterfaceConsumerViaNuGet/Person.cs new file mode 100644 index 0000000..fba8ddf --- /dev/null +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/Person.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; + +namespace ProxyInterfaceConsumer +{ + public class Person + { + private int PrivateId { get; } + public int Id { get; } + + public long? NullableLong { get; } + + public string Name { get; set; } + + public Address Address { get; set; } + + public List
AddressesLIst { get; set; } + + public Dictionary AddressesDict { get; set; } = new Dictionary(); + + public E E { get; set; } + + public IMyInterface MyInterface { get; set; } + + public int Add(string s) + { + return 600; + } + + public void AddAddress(Address a) + { + AddressesDict.Add($"{AddressesDict.Count}", a); + } + + public void Void() + { + } + } + + public enum E + { + V1, + V2 + } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/Program.cs b/src-examples/ProxyInterfaceConsumerViaNuGet/Program.cs new file mode 100644 index 0000000..0a57c69 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/Program.cs @@ -0,0 +1,25 @@ +using System; +using System.Text.Json; + +namespace ProxyInterfaceConsumer +{ + class Program + { + private static JsonSerializerOptions JsonSerializerOptions = new () + { + WriteIndented = true + }; + + static void Main(string[] args) + { + IPerson p = new PersonProxy(new Person()); + p.Name = "test"; + var ap = new AddressProxy(new Address { HouseNumber = 42 }); + p.Address = ap; + p.AddAddress(ap); + p.AddAddress(new AddressProxy(new Address { HouseNumber = 1000 })); + + Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions)); + } + } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/ProxyInterfaceConsumerViaNuGet.csproj b/src-examples/ProxyInterfaceConsumerViaNuGet/ProxyInterfaceConsumerViaNuGet.csproj new file mode 100644 index 0000000..19621b8 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/ProxyInterfaceConsumerViaNuGet.csproj @@ -0,0 +1,14 @@ + + + + Exe + net5.0 + ProxyInterfaceConsumer + ProxyInterfaceConsumer + + + + + + + \ No newline at end of file