简体   繁体   English

温莎城堡:按惯例注册,开放通用

[英]Castle Windsor: Register by convention, open generics

I have an interface like so: 我有一个像这样的界面:

public interface IGenericRepository<T>

I have a base class like so: 我有一个像这样的基类:

public abstract class GenericRepository<T> : IGenericRepository<T> where T : class

I have a class like so: 我有一个像这样的课程:

public class AGenericRepository<T> : GenericRepository<T> where T : class

Without convention, I successfully registered like so: 没有约定,我成功注册为:

container.Register(
    Component.For(typeof(GenericRepository<>)).ImplementedBy(typeof(AGenericRepository<>))
);

I can successfully resolve an object like so: 我可以像这样成功解析一个对象:

var robot = container.Resolve<GenericRepository<Android>>();

However, when trying to register by convention like so: 但是,当尝试通过约定进行如下注册时:

container.Register(Classes.FromThisAssembly()
                            .BasedOn(typeof(GenericRepository<>))
                            .WithService.Base());

I cannot resolve as I did above. 我无法像上面那样解决。 What gives? 是什么赋予了?

DefaultInterfaces() only registers interfaces with a matching name. DefaultInterfaces()仅注册具有匹配名称的接口。

Matching names, means that the implementing class contains in its name the name of the interface (without the I on the front). 匹配名称,意味着实现类在其名称中包含接口的名称(前面没有I)。

http://docs.castleproject.org/Windsor.Registering-components-by-conventions.ashx http://docs.castleproject.org/Windsor.Registering-components-by-conventions.ashx

Writing an answer since this may be too long (and codeful) for a comment. 编写答案,因为这样做可能太长(且无法编写代码),无法发表评论。

Given the following code: 给出以下代码:

public interface IGenericRepository<T> {}
public abstract class GenericRepository<T> : IGenericRepository<T> where T : class {}
public class AGenericRepository<T> : GenericRepository<T> where T : class {}
public class AInstance: AGenericRepository<string>{} 

this registration works fine for me: 此注册对我来说效果很好:

var container = new WindsorContainer();
container.Register(Classes.FromThisAssembly().BasedOn(typeof (GenericRepository<>)).WithServiceBase());
var result = container.Resolve<GenericRepository<string>>();

I have a feeling that we are lacking some information regarding what classes are registered. 我感觉我们缺少一些有关注册什么课程的信息。


EDIT: in the proposed code apparently the abstract base class acts as a stop gap to determine what the base service is. 编辑:在建议的代码中,显然抽象基类充当确定基本服务是什么的停顿。 If you use the following registration the resolution works: 如果您使用以下注册,则该解析有效:

var container = new WindsorContainer();
container.Register(Classes.FromThisAssembly().BasedOn(typeof (GenericRepository<>)).WithServiceAllInterfaces());
var result = container.Resolve<IGenericRepository<string>>();

However the resolution against the GenericRepository doesn't seem to work because it is not registered as a resolution component in castle. 但是,针对GenericRepository的解析似乎不起作用,因为它没有在城堡中注册为解析组件。 If you want to self register components you can describe it directly: 如果要自注册组件,可以直接描述它:

var container = new WindsorContainer();
container.Register(Classes.FromThisAssembly().BasedOn(typeof (GenericRepository<>)).WithServices(typeof(GenericRepository<>)));
var result = container.Resolve<GenericRepository<string>>();
// result is AGenericRepository<string>

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

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