Skip to content

Commit

Permalink
docs: Add sample project about dependency injection via constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed Mar 20, 2024
1 parent e76cb3b commit 1d0a085
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 5 deletions.
14 changes: 10 additions & 4 deletions CPlugin.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions samples/Contracts/ITestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Example.Contracts;

public interface ITestService
{
string Execute();
}
10 changes: 10 additions & 0 deletions samples/HostApplications/WebApi/Controllers/ServiceController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Example.HostWebApi.Controllers;

[ApiController]
[Route("[controller]")]
public class ServiceController
{
[HttpGet]
public ActionResult<string> Get(ITestService service)
=> service.Execute();
}
2 changes: 2 additions & 0 deletions samples/HostApplications/WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
mvcBuilder.PartManager.ApplicationParts.Add(new AssemblyPart(assembly));
}

builder.Services.AddSubtypesOf<ITestService>(ServiceLifetime.Transient);

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
Expand Down
4 changes: 3 additions & 1 deletion samples/HostApplications/WebApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
}
},
"AllowedHosts": "*",
"ServiceName": "TestService",
"Plugins": [
"Example.AppointmentPlugin.dll",
"Example.PersonPlugin.dll"
"Example.PersonPlugin.dll",
"Example.DependencyInjectionPlugin.dll"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutDir>$(WebApiProjectDir)</OutDir>
<OutputType>Library</OutputType>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
</PropertyGroup>

</Project>
3 changes: 3 additions & 0 deletions samples/Plugins/DependencyInjectionPlugin/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using Example.Contracts;
global using CPlugin.Net;
global using Example.DependencyInjectionPlugin;
23 changes: 23 additions & 0 deletions samples/Plugins/DependencyInjectionPlugin/TestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[assembly: Plugin(typeof(TestService))]

namespace Example.DependencyInjectionPlugin;

public class TestService : ITestService
{
private readonly ILogger<TestService> _logger;
private readonly IConfiguration _configuration;

public TestService(
ILogger<TestService> logger,
IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}

public string Execute()
{
_logger.LogInformation("TestService");
return _configuration["ServiceName"];
}
}
19 changes: 19 additions & 0 deletions samples/Test/WebApi/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Program>();
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);
}
}

0 comments on commit 1d0a085

Please sign in to comment.