代码之家  ›  专栏  ›  技术社区  ›  Nick Johnson

带参数的MVCContrib Windsor设置组件

  •  2
  • Nick Johnson  · 技术社区  · 16 年前

    我和温莎城堡一起使用mvcontrib图书馆,我遇到了一个问题 在注册组件时设置参数。

    对于包装DataContext的类,我有以下接口。我想要 以便能够指定要用于不同服务的DataContext,因为 我正在连接多个数据库以检索数据。

    public interface IDataContext
    {
        DataContext Context { get; }
    }
    
    public interface IReportingDC : IDataContext
    {
    }
    
    public class Repository<T> : IRepository<T> where T : class
    {
    

    公共IDataContext dc_get;set;

      public Repository(IDataContext dataContext)
      {
        DC = dataContext;
      }
    }
    

    这是我的global.asax.cs的注册行。

    container.AddComponentLifeStyle<IDataContext, MainDataContext>(Castle.Core.LifestyleType.PerWebRequest);
    
    container.AddComponentLifeStyle<IReportingDC, ReportingDC>(Castle.Core.LifestyleType.PerWebRequest);
    
    container.Register(Component.For<IRepository<ReportingTotals>>()
    .ImplementedBy<Repository<ReportingTotals>>()
    .Parameters(Parameter.ForKey("dataContext").Eq("IReportingDC"))
    .LifeStyle.PerWebRequest
    );
    

    当我尝试加载页面时,会出现以下错误。

    “参数dataContext的键无效。因此内核无法 覆盖服务依赖项“

    1 回复  |  直到 16 年前
        1
  •  4
  •   Mauricio Scheffer    16 年前

    为组件命名并使用ServiceOverrides而不是参数:

    Component.For<IReportingDC>()
             .ImplementedBy<ReportingDC>()
             .Named("IReporting")
             .LifeStyle.PerWebRequest
    

    Component.For<IRepository<ReportingTotals>>()
             .ImplementedBy<Repository<ReportingTotals>>()
             .ServiceOverrides(ServiceOverride.ForKey("dataContext").Eq("IReporting"))
    

    fluent API docs 供参考。