温莎城堡:问题与多个构造 [英] Castle Windsor: Problem with Multiple Constructors

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

问题描述

长的时间阅读器第一次在这里的作家。我目前正在从以使用Ninject到温莎城堡的当前版本为一个简单的C#.NET应用程序的转换。

Long-time reader first time writer here. I am currently undertaking a conversion from to the use of Ninject to the current release of Castle Windsor for a simple C# .NET application.

有关的大多数情况下,在转换已经井和容器的实施完美无缺执行。但我有一个小问题,我的资源库对象

For the most part, the conversion has gone well and the implementation of the containers has executed flawlessly. I am however having a small issue with my repository objects.

我有一个在下面的方式编码的用户​​信息库的对象:

I have a user repository object that is coded in the following fashion:

public class UserRepository : IUserRepository {
    public UserRepository(IObjectContext objectContext)  {
        // Check that the supplied arguments are valid.
        Validate.Arguments.IsNotNull(objectContext, "objectContext");

        // Initialize the local fields.
        ObjectContext = objectContext;
    }

    public UserRepository(IObjectContextFactory factory) 
        : this(factory.CreateObjectContext()) { 
    }

    // -----------------------------------------------
    // Insert methods and properties...
    // -----------------------------------------------
}

要符合此代码,我已经安装在我的应用程序的配置文件中的以下条目:

To correspond to this code, I have setup the following entries in my application's configuration file:

<castle>
    <components>
        <component id="objectContextFactory" lifestyle="custom"
                   customLifestyleType="Common.Infrastructure.PerWebRequestLifestyleManager, Common.Castle"
                   service="Project.DAL.Context.IObjectContextFactory, Project.DAL.LINQ"
                   type="project.DAL.Context.ObjectContextFactory, Project.DAL.LINQ">
        </component>
        <component id="userRepository" lifestyle="custom"
                   customLifestyleType="Common.Infrastructure.PerWebRequestLifestyleManager, Common.Castle"
                   service="Project.BL.Repository.IUserRepository, Project.BL"
                   type="Project.BL.Repository.UserRepository, Project.BL.LINQ">
            <parameters>
              <factory>${objectContextFactory}</factory>
            </parameters>
        </component>
    </components>
</castle>



对我来说,一切看起来像它应该。当我尝试解决IObjectContextFactory服务的实例,我检索ObjectContextFactory对象。我的问题进来时,我尝试和解决IUserRepository服务的实例。我处理以下愉快的异常:

To me, everything looks like it should. When I attempt to resolve an instance of the IObjectContextFactory service, I retrieve an ObjectContextFactory object. My problem comes in when I try and resolve an instance of the IUserRepository service. I am treated to the following delightful exception:

无法创建组件'userRepository,因为它有依赖关系得到满足。
userRepository正在等待以下依赖性:

Can't create component 'userRepository' as it has dependencies to be satisfied. userRepository is waiting for the following dependencies:

服务:

- 这是未注册SandCastle.DAL.Context.IObjectContext

我试着尽我所能想到这一点。所以,你们StackOverflow的读者,我说:有什么想法

I've tried everything I can think of on this. So, unto you stackoverflow readers, I say: got any ideas?

推荐答案

这可能会被视为一个bug(确实,类似案例,这是可以解决的),但它kindof一个由设计功能。

This might be regarded as a bug (and indeed for cases like this it's fixable) but it's kindof a by-design feature.

温莎尝试匹配的贪婪的构造函数(最参数)能够满足。

Windsor tries to match the greediest constructor (one with the most parameters) it can satisfy.

然而,在你的情况,还有一些具有参数(一)人数最多两个构造,所以温莎刚刚拿起的第一个,哪里有什么第一的意思是未定义如果您打开构造的顺序在源代码中的代码将开始工作。

However in your case, there are two constructors that have the greatest number of parameters (of one), so Windsor just picks the first, where what the "first" means is undefined.

事实上,尽管这是一个黑客,依靠无证行为,不这样做

indeed if you switch the order of your constructors in your source code your code will start working, although it's a hack, relying on undocumented behavior and don't do it.

让我们回到我们开始的地方好吗?

Let's go back to where we started shall we?

我说温莎是困惑,因为没有< STRONG>单贪婪的构造能够满足。

I said Windsor is confused because there's no single greediest constructor it can satisfy.

快速而明确的解决将是一个假的参数添加到其中的一个构造函数,让他们有不同数量的参数:

Quick and well-defined fix would be to add a fake parameter to one of th constructors so that they have different numbers of parameters:

public class UserRepository : IUserRepository {
    public UserRepository(IObjectContext objectContext, object fakeIgnoreMe)  {
        // Check that the supplied arguments are valid.
        Validate.Arguments.IsNotNull(objectContext, "objectContext");
        // ignoring fake additional argument
        // Initialize the local fields.
        ObjectContext = objectContext;
    }

    public UserRepository(IObjectContextFactory factory) 
        : this(factory.CreateObjectContext()) { 
    }

    // -----------------------------------------------
    // Insert methods and properties...
    // -----------------------------------------------
}

请将此问题报告给城堡的用户列表或的直奔问题跟踪这样它会得到解决。

Please report this issue to Castle users list or straight to issue tracker so that it will get fixed.

这篇关于温莎城堡:问题与多个构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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