2016-12-02 31 views
0

我想實現對象的構造如下圖所示,城堡溫莎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 
    { 
    } 
} 

回答

1

您需要查看範圍;目前你沒有指定一個範圍,所以所有的依賴都是單例。 註冊DBContext以及​​,IParserIProcessor所有與Scoped生命。例如。

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

然後,您需要解決您的依賴關係並在範圍內使用它們。這通常對基礎設施進行管理,但在其最簡單的是這樣的:

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

現在,一個新DBContext將每個範圍創建和配置當容器範圍結束。您不必擔心初始化DBContext或將其傳遞給Resolve方法。

+0

這很好用。感謝那。 – dezzy