国际奥委会与IValidationDictionary温莎城堡 [英] IOC on IValidationDictionary with Castle Windsor

查看:192
本文介绍了国际奥委会与IValidationDictionary温莎城堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的温莎城堡和我只是使用最新版本。我已经创建了哪些工作正常我的资料库条目,但我有我路过到我的控制器的一个最后的依赖。

I'm new to Castle Windsor and am just using the latest version. I've created entries for my repositories which are working fine but I have one final dependency that I'm passing into my controller.

我创建从IValidationDictionary继承的ModelStateWrapper。该ModelStateWrapper需要ModelStateDictionary在它的构造函数,这样在我的code,我可以通过以下为例:

I've created a ModelStateWrapper which inherits from IValidationDictionary. The ModelStateWrapper takes a ModelStateDictionary in it's constructor so that in my code I can pass the following as an example:

IMembershipService _memSvc;
IValidationDictionary _validationService;

public AccountController()
{
    _validationService = new ModelStateWrapper(this.ModelState);
    _memSvc = new MembershipService(_validationService);
}

在我的测试中,我可以再做到这一点使用起订量:

In my tests I can then do this using Moq:

var v = new Mock<ModelStateDictionary>();
_validationService = new ModelStateWrapper(v.Object);
_service = new MembershipService(_validationService);

我似乎无法让岛城与ModelStateWrapper注入的ModelState。我不知道从哪里开始,似乎我不能只是'忽略它并尝试手动注入作为城堡正在搜索的依赖和投掷了我一个错误说一个依赖性剩余。

I can't seem to get Castle to inject ModelState in with the ModelStateWrapper. I have no idea where to start and it seems I can't just 'ignore it' and try to manually inject as Castle is searching for dependencies and throwing me an error saying a dependency is remaining.

如何配置温莎城堡为使用基于ModelStateWrapper关闭IValidationDictionary还包括ModelState中作为构造函数的参数?

How do I configure Castle Windsor to use the ModelStateWrapper based off IValidationDictionary and also include ModelState as the constructor parameter?

劳埃德

推荐答案

这似乎是你有一个循环依赖(从来都不是一件好事)。您可以通过使用抽象工厂为<一个绕过它href=\"http://stackoverflow.com/questions/1453128/is-there-a-good-proper-way-of-solving-the-dependency-injection-loop-problem-in-th/1453242#1453242\">described在此非常类似的问题。

It seems like you have a circular dependency (never a good thing). You can get around it by using an Abstract Factory as described in this very similar question.

不过,虽然你可能能够解决这样的问题,倒不如重新设计的API,使循环依赖消失。循环依赖往往预示一个设计缺陷。

However, although you may be able to solve the problem like this, it would be better to redesign the API to make the circular dependency go away. Circular dependencies often indicate a design flaw.

这篇关于国际奥委会与IValidationDictionary温莎城堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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