I am new to dryioc so excuse the ignorance:)
I would like to understand what the correct approach would be for the following,
Creating a console app that creates instance of dryioc container. Register a logger in the container (as singleton). Then instantiate a class from a class library (separate project) to be used in the console app and be able to reference the container (main app) from the class library to get an instance of logger. So the class library will utilise any logger registered in console app.
I'd prefer not to pass container as part of constructors in class library.
Thanks for any assistance.
Something like this. Probably true for any other container (or even without container):
namespace Core
{
public interface ILogger
{
void Log(string message);
}
}
namespace Lib
{
using Core;
public class Foo
{
readonly ILogger _logger;
public Foo(ILogger logger)
{
_logger = logger;
}
public void DoSomething()
{
_logger.Log("About to do something ...");
}
}
}
namespace ConsoleApp
{
using System;
using Core;
using Lib;
using DryIoc;
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
public class Program
{
public static void Main()
{
var c = new Container();
c.Register<ILogger, ConsoleLogger>(Reuse.Singleton);
c.Register<Foo>();
var foo = c.Resolve<Foo>();
foo.DoSomething();
}
}
}