Initial files
This commit is contained in:
14
DISandbox/DISandbox.csproj
Normal file
14
DISandbox/DISandbox.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\QuickDI\QuickDI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
44
DISandbox/Program.cs
Normal file
44
DISandbox/Program.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using QuickDI;
|
||||
|
||||
namespace DISandbox;
|
||||
|
||||
interface IService {
|
||||
public void Log(string message);
|
||||
}
|
||||
|
||||
class Service : IService {
|
||||
public void Log(string message) {
|
||||
Console.WriteLine($"[Service] {message}");
|
||||
}
|
||||
}
|
||||
|
||||
interface IOtherService {
|
||||
public Guid Id { get; }
|
||||
|
||||
public void ShowId() => Console.WriteLine($"[OtherService] ID: {Id}");
|
||||
}
|
||||
|
||||
class GuidService : IOtherService {
|
||||
public Guid Id { get; } = Guid.NewGuid();
|
||||
}
|
||||
|
||||
class GuidDependantService(IService logService, IOtherService guidService) {
|
||||
public void LogWithId(string message) {
|
||||
logService.Log($"[GuidDependantService] {message} (ID: {guidService.Id})");
|
||||
}
|
||||
}
|
||||
|
||||
static class Program {
|
||||
static void Main(string[] args) {
|
||||
var container = new ServiceContainer()
|
||||
.AddSingleton<IService, Service>()
|
||||
.AddTransient<IOtherService, GuidService>()
|
||||
.AddTransient<GuidDependantService, GuidDependantService>();
|
||||
|
||||
var service = container.GetService<IService>();
|
||||
service.Log("Hello, Dependency Injection!");
|
||||
container.GetService<IOtherService>().ShowId();
|
||||
container.GetService<GuidDependantService>().LogWithId("Hello, sent from the dependency.");
|
||||
container.GetService<IService>().Log("Goodbye, Dependency Injection!");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user