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
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 20 additions & 0 deletions
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>
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 25 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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}");
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 31 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)