Castle Windsor:多个构造函数的问题 [英] Castle Windsor: Problem with Multiple Constructors

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

问题描述

长时间的读者第一次写作这里。我目前正在从一个简单的C#.NET应用程序转换到使用Ninject到当前版本的Castle Windsor。



在大多数情况下,转换已经顺利进行,容器的执行完美无缺。



我有一个以下列方式编码的用户存储库对象:

  public class UserRepository:IUserRepository {
public UserRepository(IObjectContext objectContext){
//检查提供的参数是否有效。
Validate.Arguments.IsNotNull(objectContext,objectContext);

//初始化本地字段。
ObjectContext = objectContext;
}

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

// - ---------------------------------------------
/ /插入方法和属性...
// ------------------------------------ -----------
}

对应此代码,我在我的应用程序的配置文件中设置了以下条目:

 < castle& 
< components>
< component id =objectContextFactorylifestyle =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 =userRepositorylifestyle =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服务的实例。我处理以下令人愉快的异常:


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



服务:



- 未注册的SandCastle.DAL.Context.IObjectContext。


想想这个。所以,对你stackoverflow读者,我说:有任何想法?

解决方案

这可能被认为是一个错误



Windsor试图匹配最贪婪的构造函数(一个具有最多的参数),它可以满足。 p>

但是在你的情况下,有两个构造函数有最多的参数(一个),所以Windsor只是选择第一个,其中第一的意思是未定义。



确实,如果您在源代码中切换构造函数的顺序,代码将开始工作,虽然这是一个黑客,依赖于未记录的行为,不做



我们回到我们开始的地方吧?



我说Windsor很困惑,因为没有<



快速定义明确的修复方法是在一个构造函数中添加一个伪参数,这样它们就可以不同数量的参数:

  public class UserRepository:IUserRepository {
public UserRepository(IObjectContext objectContext,object fakeIgnoreMe){
//检查提供的参数是否有效。
Validate.Arguments.IsNotNull(objectContext,objectContext);
//忽略伪附加参数
//初始化本地字段。
ObjectContext = objectContext;
}

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

// - ---------------------------------------------
/ /插入方法和属性...
// ------------------------------------ -----------
}

请将此问题报告给城堡用户列表直接发出问题跟踪器,以便它将得到修复。


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>

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:

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

Services:

- SandCastle.DAL.Context.IObjectContext which was not registered.

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

解决方案

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?

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.

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

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