MvcContrib温莎安装组件随着参数 [英] MvcContrib Windsor Setup Component With Parameter

查看:119
本文介绍了MvcContrib温莎安装组件随着参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是MvcContrib库温莎城堡,我有一个问题
与设置参数,当我注册一个组件。

I am using the MvcContrib library with Castle Windsor and I am having a problem with setting a parameter when I register a component.

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

I have the following interfaces for classes that wrap a DataContext. I want to be able to specify which DataContext to use for different services because I am connecting to several databases to retrieve data.

public interface IDataContext
{
    DataContext Context { get; }
}

public interface IReportingDC : IDataContext
{
}

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

公共IDataContext DC {搞定;组; }

public IDataContext DC { get; set; }

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

下面是注册线从我的global.asax.cs。

Here are the registration lines from my 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
);

当我尝试和加载页面我碰到下面的错误。

When I try and load the page I get the following error.

键无效参数DataContext的,因此内核无法
 覆盖服务依赖

"Key invalid for parameter dataContext. Thus the kernel was unable to override the service dependency"

推荐答案

命名您的组件,并使用ServiceOverrides代替参数:

Name your component and use ServiceOverrides instead of Parameters:

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

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

查看流利的API文档,以供参考。

这篇关于MvcContrib温莎安装组件随着参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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