Sting - C# IoC container for dependency injection
using Sting;
var container = new Container();
interface ILog { void Info(string text); }
class Log : Ilog
{
public void Info(string text)
{
Console.WriteLine(text);
}
}
container.Register<ILog, Log>();
var logger = container.Resolve<ILog>();
logger.Info("Hello");
container.RegisterSingleton<ILog, Log>();
var logger1 = container.Resolve<ILog>();
var logger2 = container.Resolve<ILog>();
Assert.True(logger1 == logger2);
interface IPublisher { void Publish(string message); }
class MyPublisher : IPublisher
{
private ILog Log { get; }
public MyPublisher(ILog log)
{
Log = log;
}
public void Publish(string message)
{
Log.Info($"Publishing message: {message}");
// publish...
}
}
container.Register<ILog, Log>();
container.Register<IPublisher, MyPublisher>();
var publisher = container.Resolve<IPublisher>();
publisher.Publish("Hello");