温莎城堡传播内联依赖 [英] Castle Windsor propagating inline dependencies

查看:37
本文介绍了温莎城堡传播内联依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现如下所示的对象构造,

I am trying to achieve object construction as shown below,

using (var context = new DbContext())
{
    var processor = new Processor(context, new Parser(context, new Logger(context)), new Logger(context));
}

,但使用温莎城堡。我正在使用内联依赖性,如下面的代码所示,但是正如Castle Windsor文档指出的那样:内联依赖性不会传播 城堡温莎传递参数。我该怎么做呢?

but using Castle Windsor. I am using inline dependencies as the code shows below, but as the Castle Windsor documentation states "Inline dependencies don't get propagated" Castle Windsor passing arguments. How can I achieve this another way?

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
    {
    }
}


推荐答案

您需要研究范围;目前,您尚未指定范围,因此所有依赖项均为单例。
注册 DBContext 以及 ILogger IParser ,以及 IProcessor 都具有 Scoped 生命线。 EG

You need to look at scoping; at the moment you're not specifying a scope so all dependencies will be singletons. Register DBContext along with ILogger, IParser, and IProcessor all with the Scoped lifestlye. E.G.

container.Register(Component.For<DBContext>()
    .ImplementedBy<DBContext>()
    .Lifestyle.Scoped);

然后,您需要解析依赖项并在范围内使用它们。通常,这可以在基础架构中进行管理,但最简单的形式是:

Then you need to resolve your dependencies and use them within a scope. This would normally be managed in infrastructure, but at its simplest would look like this:

using(container.BeginScope()) 
{
    var processor = container.Resolve<IProcessor>();
    // use processor here.
}

现在有了新的 DBContext 将根据作用域创建,并在容器作用域结束时处置。您不必担心初始化 DBContext 或将其传递给 Resolve 方法。

Now a new DBContext will be created per scope and disposed when the container scope ends. You don't have to worry about initialising the DBContext or passing it to the Resolve method.

这篇关于温莎城堡传播内联依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆