Add ProxyInterfaceConsumerViaNuGet project
This commit is contained in:
@@ -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}
|
||||
|
||||
@@ -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
|
||||
[](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";
|
||||
```
|
||||
```
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
public interface IMyInterface
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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<IClazz> { 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,13 +7,12 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
|
||||
<!--<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.10.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
|
||||
<!--<PackageReference Include="TinyMapper" Version="3.0.3" />-->
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />-->
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
public class Address
|
||||
{
|
||||
public int HouseNumber { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Address))]
|
||||
public partial interface IAddress
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
public interface IMyInterface
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Person))]
|
||||
public partial interface IPerson
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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<Address> AddressesLIst { get; set; }
|
||||
|
||||
public Dictionary<string, Address> AddressesDict { get; set; } = new Dictionary<string, Address>();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<AssemblyName>ProxyInterfaceConsumer</AssemblyName>
|
||||
<RootNamespace>ProxyInterfaceConsumer</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ProxyInterfaceGenerator" Version="0.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user