Skip to content

Commit 0c23a01

Browse files
committed
Add example project using library with dependency injection.
1 parent e9206ad commit 0c23a01

11 files changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Examples.DependencyInjection;
2+
3+
public class AnimalProcessorInput
4+
{
5+
public required string Animal { get; set; }
6+
public required string Color { get; set; }
7+
public required int Height { get; set; }
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Examples.DependencyInjection;
2+
3+
public class CatsOnlyProcessor : IAnimalProcessor
4+
{
5+
public bool Condition(AnimalProcessorInput input) => input.Animal == "cat";
6+
7+
public string Process(AnimalProcessorInput input) {
8+
return "animal is a cat!";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Examples.DependencyInjection;
2+
3+
public class DefaultProcessor : IAnimalProcessor
4+
{
5+
public bool Condition(AnimalProcessorInput input) => true;
6+
7+
public string Process(AnimalProcessorInput input) {
8+
return "it is an animal";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<PublishAot>True</PublishAot>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\LightChain\LightChain.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using LightChain;
2+
3+
namespace Examples.DependencyInjection;
4+
5+
public interface IAnimalProcessor : IProcessor<AnimalProcessorInput, string>
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using LightChain;
2+
3+
namespace Examples.DependencyInjection;
4+
5+
public class Main
6+
{
7+
private readonly IChain<AnimalProcessorInput, string> animalProcessor;
8+
9+
public Main(IChain<AnimalProcessorInput, string> animalProcessor) {
10+
this.animalProcessor = animalProcessor;
11+
}
12+
13+
public string Run() {
14+
var input = new AnimalProcessorInput
15+
{
16+
Animal = "dog",
17+
Color = "red",
18+
Height = 100,
19+
};
20+
21+
var result = animalProcessor.Run(input);
22+
23+
return result;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using Examples.DependencyInjection;
3+
4+
Console.WriteLine("Starting App...");
5+
6+
var result = new Startup().Run();
7+
8+
Console.WriteLine($"Result is: {result}");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Examples.DependencyInjection;
2+
3+
public class RedOnlyProcessor : IAnimalProcessor
4+
{
5+
public bool Condition(AnimalProcessorInput input) => input.Color == "red";
6+
7+
public string Process(AnimalProcessorInput input) {
8+
return "animal is red!";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using LightChain;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace Examples.DependencyInjection;
5+
6+
internal static class ServiceRegistrations
7+
{
8+
public static void AddServices(this IServiceCollection services) {
9+
services.AddTransient<IAnimalProcessor, CatsOnlyProcessor>();
10+
services.AddTransient<IAnimalProcessor, RedOnlyProcessor>();
11+
services.AddTransient<IAnimalProcessor, DefaultProcessor>();
12+
13+
services.AddTransient<IChain<AnimalProcessorInput, string>, Chain<IAnimalProcessor, AnimalProcessorInput, string>>();
14+
15+
services.AddTransient<Main>();
16+
}
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace Examples.DependencyInjection;
5+
6+
public class Startup
7+
{
8+
public string Run() {
9+
Main main;
10+
11+
using (
12+
var host = CreateHostBuilder().Build()
13+
) {
14+
using var scope = host.Services.CreateScope();
15+
var services = scope.ServiceProvider;
16+
17+
main = services.GetRequiredService<Main>();
18+
}
19+
20+
var result = main.Run();
21+
22+
return result;
23+
}
24+
25+
private static IHostBuilder CreateHostBuilder() {
26+
return Host.CreateDefaultBuilder()
27+
.ConfigureServices((_, services) => {
28+
services.AddServices();
29+
});
30+
}
31+
}

LightChain.sln

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LightChain", "LightChain\Li
1313
EndProject
1414
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LightChain.UnitTests", "LightChain.UnitTests\LightChain.UnitTests.csproj", "{51E06062-8E93-4849-BCC0-4CCFF756256A}"
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples.DependencyInjection", "Examples\Examples.DependencyInjection\Examples.DependencyInjection.csproj", "{88166DED-9CBD-4B3F-843B-18247176E281}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{51E06062-8E93-4849-BCC0-4CCFF756256A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{51E06062-8E93-4849-BCC0-4CCFF756256A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{51E06062-8E93-4849-BCC0-4CCFF756256A}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{88166DED-9CBD-4B3F-843B-18247176E281}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{88166DED-9CBD-4B3F-843B-18247176E281}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{88166DED-9CBD-4B3F-843B-18247176E281}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{88166DED-9CBD-4B3F-843B-18247176E281}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)