From 1d0a085ae114aaa3f98e3412b65c098aee1eb69d Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Tue, 19 Mar 2024 19:17:37 -0500 Subject: [PATCH] docs: Add sample project about dependency injection via constructor --- CPlugin.Net.sln | 14 +++++++---- samples/Contracts/ITestService.cs | 6 +++++ .../WebApi/Controllers/ServiceController.cs | 10 ++++++++ samples/HostApplications/WebApi/Program.cs | 2 ++ .../HostApplications/WebApi/appsettings.json | 4 +++- .../Example.DependencyInjectionPlugin.csproj | 9 ++++++++ .../DependencyInjectionPlugin/GlobalUsings.cs | 3 +++ .../DependencyInjectionPlugin/TestService.cs | 23 +++++++++++++++++++ samples/Test/WebApi/Get.cs | 19 +++++++++++++++ 9 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 samples/Contracts/ITestService.cs create mode 100644 samples/HostApplications/WebApi/Controllers/ServiceController.cs create mode 100644 samples/Plugins/DependencyInjectionPlugin/Example.DependencyInjectionPlugin.csproj create mode 100644 samples/Plugins/DependencyInjectionPlugin/GlobalUsings.cs create mode 100644 samples/Plugins/DependencyInjectionPlugin/TestService.cs diff --git a/CPlugin.Net.sln b/CPlugin.Net.sln index c4aa9cb..4d4877e 100644 --- a/CPlugin.Net.sln +++ b/CPlugin.Net.sln @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.HostWebApi", "sampl EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SharedEntities", "samples\SharedEntities\Example.SharedEntities.csproj", "{F66A1430-3F32-4E25-8966-54D502D216DE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.DependencyInjectionPlugin", "samples\Plugins\DependencyInjectionPlugin\Example.DependencyInjectionPlugin.csproj", "{28065D77-B890-47DE-B695-04E388176925}" +EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.JsonPlugin", "samples\Plugins\JsonPlugin\Example.JsonPlugin.csproj", "{C5B8EF73-7DB5-441F-AE38-0988751A896B}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.OldJsonPlugin", "samples\Plugins\OldJsonPlugin\Example.OldJsonPlugin.csproj", "{1ADE3B86-00EF-4976-8B67-09B360B149FA}" @@ -98,6 +100,10 @@ Global {18534944-583B-4924-AC5B-E0655FD92AAC}.Debug|Any CPU.Build.0 = Debug|Any CPU {18534944-583B-4924-AC5B-E0655FD92AAC}.Release|Any CPU.ActiveCfg = Release|Any CPU {18534944-583B-4924-AC5B-E0655FD92AAC}.Release|Any CPU.Build.0 = Release|Any CPU + {1E64908D-DC48-4B83-BB25-CF36821EB37F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1E64908D-DC48-4B83-BB25-CF36821EB37F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1E64908D-DC48-4B83-BB25-CF36821EB37F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1E64908D-DC48-4B83-BB25-CF36821EB37F}.Release|Any CPU.Build.0 = Release|Any CPU {0F27C776-F284-4C94-86C9-0FF089245E13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {0F27C776-F284-4C94-86C9-0FF089245E13}.Debug|Any CPU.Build.0 = Debug|Any CPU {0F27C776-F284-4C94-86C9-0FF089245E13}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -122,10 +128,10 @@ Global {0BCD3305-F0D5-43E6-B879-EEF0827558A8}.Debug|Any CPU.Build.0 = Debug|Any CPU {0BCD3305-F0D5-43E6-B879-EEF0827558A8}.Release|Any CPU.ActiveCfg = Release|Any CPU {0BCD3305-F0D5-43E6-B879-EEF0827558A8}.Release|Any CPU.Build.0 = Release|Any CPU - {1E64908D-DC48-4B83-BB25-CF36821EB37F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1E64908D-DC48-4B83-BB25-CF36821EB37F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1E64908D-DC48-4B83-BB25-CF36821EB37F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1E64908D-DC48-4B83-BB25-CF36821EB37F}.Release|Any CPU.Build.0 = Release|Any CPU + {28065D77-B890-47DE-B695-04E388176925}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {28065D77-B890-47DE-B695-04E388176925}.Debug|Any CPU.Build.0 = Debug|Any CPU + {28065D77-B890-47DE-B695-04E388176925}.Release|Any CPU.ActiveCfg = Release|Any CPU + {28065D77-B890-47DE-B695-04E388176925}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/samples/Contracts/ITestService.cs b/samples/Contracts/ITestService.cs new file mode 100644 index 0000000..c444cf0 --- /dev/null +++ b/samples/Contracts/ITestService.cs @@ -0,0 +1,6 @@ +namespace Example.Contracts; + +public interface ITestService +{ + string Execute(); +} diff --git a/samples/HostApplications/WebApi/Controllers/ServiceController.cs b/samples/HostApplications/WebApi/Controllers/ServiceController.cs new file mode 100644 index 0000000..3566475 --- /dev/null +++ b/samples/HostApplications/WebApi/Controllers/ServiceController.cs @@ -0,0 +1,10 @@ +namespace Example.HostWebApi.Controllers; + +[ApiController] +[Route("[controller]")] +public class ServiceController +{ + [HttpGet] + public ActionResult Get(ITestService service) + => service.Execute(); +} diff --git a/samples/HostApplications/WebApi/Program.cs b/samples/HostApplications/WebApi/Program.cs index 3bbb2a2..18e28da 100644 --- a/samples/HostApplications/WebApi/Program.cs +++ b/samples/HostApplications/WebApi/Program.cs @@ -18,6 +18,8 @@ mvcBuilder.PartManager.ApplicationParts.Add(new AssemblyPart(assembly)); } +builder.Services.AddSubtypesOf(ServiceLifetime.Transient); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => diff --git a/samples/HostApplications/WebApi/appsettings.json b/samples/HostApplications/WebApi/appsettings.json index a9d4f70..45de423 100644 --- a/samples/HostApplications/WebApi/appsettings.json +++ b/samples/HostApplications/WebApi/appsettings.json @@ -6,8 +6,10 @@ } }, "AllowedHosts": "*", + "ServiceName": "TestService", "Plugins": [ "Example.AppointmentPlugin.dll", - "Example.PersonPlugin.dll" + "Example.PersonPlugin.dll", + "Example.DependencyInjectionPlugin.dll" ] } diff --git a/samples/Plugins/DependencyInjectionPlugin/Example.DependencyInjectionPlugin.csproj b/samples/Plugins/DependencyInjectionPlugin/Example.DependencyInjectionPlugin.csproj new file mode 100644 index 0000000..0f84721 --- /dev/null +++ b/samples/Plugins/DependencyInjectionPlugin/Example.DependencyInjectionPlugin.csproj @@ -0,0 +1,9 @@ + + + + $(WebApiProjectDir) + Library + true + + + diff --git a/samples/Plugins/DependencyInjectionPlugin/GlobalUsings.cs b/samples/Plugins/DependencyInjectionPlugin/GlobalUsings.cs new file mode 100644 index 0000000..d51cdec --- /dev/null +++ b/samples/Plugins/DependencyInjectionPlugin/GlobalUsings.cs @@ -0,0 +1,3 @@ +global using Example.Contracts; +global using CPlugin.Net; +global using Example.DependencyInjectionPlugin; diff --git a/samples/Plugins/DependencyInjectionPlugin/TestService.cs b/samples/Plugins/DependencyInjectionPlugin/TestService.cs new file mode 100644 index 0000000..4074d4e --- /dev/null +++ b/samples/Plugins/DependencyInjectionPlugin/TestService.cs @@ -0,0 +1,23 @@ +[assembly: Plugin(typeof(TestService))] + +namespace Example.DependencyInjectionPlugin; + +public class TestService : ITestService +{ + private readonly ILogger _logger; + private readonly IConfiguration _configuration; + + public TestService( + ILogger logger, + IConfiguration configuration) + { + _logger = logger; + _configuration = configuration; + } + + public string Execute() + { + _logger.LogInformation("TestService"); + return _configuration["ServiceName"]; + } +} diff --git a/samples/Test/WebApi/Get.cs b/samples/Test/WebApi/Get.cs index 98a062f..7509337 100644 --- a/samples/Test/WebApi/Get.cs +++ b/samples/Test/WebApi/Get.cs @@ -102,4 +102,23 @@ public async Task Get_WhenWeatherForecastAreObtained_ShouldReturnsHttpStatusCode result.IsSuccess.Should().BeTrue(); result.Data.Should().HaveCount(expectedWeatherForecast); } + + [Test] + public async Task Get_WhenServiceNameIsObtained_ShouldReturnsHttpStatusCodeOk() + { + // Arrange + using var factory = new WebApplicationFactory(); + var client = factory.CreateClient(); + var expectedServiceName = "TestService"; + + // Act + var httpResponse = await client.GetAsync("/Service"); + var result = await httpResponse + .Content + .ReadAsStringAsync(); + + // Asserts + httpResponse.StatusCode.Should().Be(HttpStatusCode.OK); + result.Should().Be(expectedServiceName); + } }