Garner is a simple DI container implementation for .NET.
using System;
namespace SampleApp;
public class ConsoleSayingService
{
public void Say(string phrase)
{
Console.WriteLine(phrase);
}
}
public class Tom
{
protected ConsoleSayingService _consoleSayingService;
public Tom(ConsoleSayingService consoleSayingService)
{
this._consoleSayingService = consoleSayingService;
}
public void SayName()
{
this._consoleSayingService.Say("My name is Tom.");
}
}using Garner.Interfaces;
using SampleApp;
using System.Collections.Generic;
namespace SampleApp.Factory;
public class ServicesDictionaryFactory : IServicesDictionaryFactory
{
public Dictionary<string, dynamic> createServicesDictionary()
{
var dictionary = new Dictionary<string, dynamic>()
{
["console_saying_service"] = new ConsoleSayingService(),
};
dictionary.Add("Tom", new Tom(dictionary["console_saying_service"]));
return dictionary;
}
}use Garner;
use SampleApp.Factory;
namespace SampleApp;
class Program
{
public static void Main(string[] args)
{
Container.ServicesDictionaryFactory = new ServicesDictionaryFactory();
Container container = Container.GetInstance();
Tom tom = container.Get("Tom");
tom.SayName(); // output "My name is Tom." in the console
}
}