简体   繁体   English

当还有专门的服务类型时,使用 Castle Windsor 的流畅接口在装饰器链中注册组件?

[英]Using Castle Windsor's fluent interface to register components in a decorator chain, when there are also specialised service-types?

I am trying to implement a decorator chain for my data-access based on IRepository.我正在尝试为基于 IRepository 的数据访问实现装饰器链。 I have a Repository that does the data- access (at the moment just in-memory list) {1}, and I have one that does validation {2}.我有一个执行数据访问的存储库(目前只是内存列表){1},我有一个执行验证 {2}。 On top of that, I have a specialisation of my Repository, IUsersRepository {3}, which defines another method on the base interface.最重要的是,我有一个专门的知识库,IUsersRepository {3},它定义了基本接口上的另一个方法。 I am running on the latest trunk revision 5376.我在最新的主干版本 5376 上运行。

I would like to be able to register my components using the fluent interface - is it possible to specify, as it is via XML, the key of the service to use as the innerRepo?我希望能够使用流畅的界面注册我的组件 - 是否可以像通过 XML 一样指定要用作innerRepo 的服务的键? If so, how?如果是这样,如何? (See Bitter Coder Decorator tutorial for an XML-config example). (有关 XML 配置示例,请参阅Bitter Coder Decorator 教程)。 I would prefer to not need to rely on the order of registrations, apart from anything else.除了其他任何事情外,我宁愿不需要依赖注册顺序。

I am following this blog post .我正在关注这篇博文

I have been able to get the decorator chain working if I ask the container for IRepository {4};如果我向容器询问 IRepository {4},我已经能够使装饰器链正常工作; I see my ValidatingRepository methods get called before my data-access.我看到我的 ValidatingRepository 方法在我的数据访问之前被调用。 But I have not been able to get it working if I ask the container for IUserRepository (I get an InvalidCastException - I guess this is expected, but is it work-aroundable?).但是,如果我向容器询问 IUserRepository(我得到一个 InvalidCastException - 我想这是预期的,但它是否可以解决?),我无法让它工作。

I would like to avoid needing to use one decorated-repo and one specialised-methods repository in my services;我想避免在我的服务中使用一个装饰存储库和一个专用方法存储库; this strikes me as error-prone.这让我觉得容易出错。 At the moment, my specialised methods are all to do with fetching entities by different criteria - I suppose I could make my Get method take a detached-criteria - that would allow me to only have one on my interface, and eliminate the need for the specialised interfaces.目前,我的专用方法都是通过不同的标准来获取实体——我想我可以让我的 Get 方法采用分离标准——这将允许我的界面上只有一个,并消除对专用接口。 Except that I'm not allowed to because we haven't decided to definitely use NHibernate.除了我不被允许,因为我们还没有决定绝对使用 NHibernate。 Perhaps if I made IRepo.Get take a delegate or an IQueryable?也许如果我让 IRepo.Get 接受委托或 IQueryable? Any ideas?有任何想法吗? I've been banging my head against this all morning and wouldn't be surprised if I have tunnel vision, and there's a different, better, approach;我整个上午都在用头撞这个,如果我有狭隘的视野,我也不会感到惊讶,而且还有一种不同的、更好的方法; any suggestions are welcomed!欢迎任何建议!

{1} - {4} can be seen at utilitybase 's copy/paste facility. {1} - {4} 可以在实用程序库的复制/粘贴工具中看到。

此处交叉发布(附有解决方案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM