本文介绍了温莎城堡与多个构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我目前正在进行一个简单的C#.NET应用程序的转换,从使用Ninject到最新版本的Castle Windsor。 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:服务: -未注册的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?推荐答案这可能被认为是一个错误(对于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尝试匹配它可以满足的最贪婪的构造函数(参数最多的一个)。Windsor tries to match the greediest constructor (one with the most parameters) it can satisfy.但是,在您的情况下,有两个构造函数的参数数量最多(一个),因此Windsor只会选择第一个,其中第一个的含义是不确定的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.实际上,如果您在源代码中切换了构造函数的顺序,则您的代码将开始工作,尽管这是一种hack,它依赖于未记录的行为并且不执行此操作。 。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.快速且定义明确的修复方法是在其中一个c中添加一个伪参数从而使它们具有不同数量的参数: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. 这篇关于温莎城堡与多个构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 18:57