【问题标题】:MvcContrib Windsor Setup Component With Parameter带参数的 MvcContrib Windsor 设置组件
【发布时间】:2010-02-24 16:32:25
【问题描述】:

我在 Castle Windsor 中使用 MvcContrib 库,但遇到了问题 注册组件时设置参数。

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

public interface IDataContext
{
    DataContext Context { get; }
}

public interface IReportingDC : IDataContext
{
}

public class Repository<T> : IRepository<T> where T : class
{

公共 IDataContext DC { 获取;放; }

  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
);

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

"key 对参数 dataContext 无效。因此内核无法 覆盖服务依赖”

【问题讨论】:

  • 请修正代码格式

标签: asp.net-mvc castle-windsor mvccontrib


【解决方案1】:

为您的组件命名并使用 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 以供参考。

【讨论】:

  • 谢谢,我刚刚发现它并让它工作了,所以我很高兴你为我确认了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
  • 2011-04-01
  • 1970-01-01
  • 2012-09-08
相关资源
最近更新 更多