Я пытаюсь добиться строительства объекта, как показано ниже,замок Виндзор зависимости propogating инлайн
using (var context = new DbContext())
{
var processor = new Processor(context, new Parser(context, new Logger(context)), new Logger(context));
}
но используя замок Виндзор. Я использую встроенные зависимости, как показано ниже в коде, но поскольку документация Castle Windsor гласит: «Внутренние зависимости не распространяются» Castle Windsor passing arguments. Как я могу добиться этого другим способом?
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using System;
namespace IOCTesting
{
class Program
{
static void Main(string[] args)
{
using (var context = new DbContext())
{
var processor = new Processor(context, new Parser(context, new Logger(context)), new Logger(context));
}
var container = new WindsorContainer();
container
.Register(Component.For<IProcessor>()
.ImplementedBy<Processor>());
container
.Register(Component.For<IParser>()
.ImplementedBy<Parser>());
container
.Register(Component.For<ILogger>()
.ImplementedBy<Logger>());
//[1] Creating my scope object here. (context)
using (var context = new DbContext())
{
var processor = container.Resolve<IProcessor>(new { context = context });
}
}
}
public class DbContext : IDisposable
{
public void Dispose()
{
Console.WriteLine("DbContext disposed.");
}
}
public class Processor : IProcessor
{
private readonly DbContext _context;
private readonly ILogger _logger;
private readonly IParser _parser;
//Dependency context passed in is the same object within the scope. See [1]
public Processor(DbContext context, IParser parser, ILogger logger)
{
_context = context;
_parser = parser;
_logger = logger;
}
}
public class Parser : IParser
{
private readonly DbContext _context;
private readonly ILogger _logger;
//Dependency context passed in is the same object within the scope. See [1]
public Parser(DbContext context, ILogger logger)
{
_context = context;
_logger = logger;
}
}
public class Logger : ILogger
{
private readonly DbContext _context;
//Dependency context passed in is the same object within the scope. See [1]
public Logger(DbContext context)
{
_context = context;
}
}
public interface IProcessor
{
}
public interface IParser
{
}
public interface ILogger
{
}
}
Это работает красиво. Спасибо за это. – dezzy