是否可以在不指定接口的情况下在Windsor中注册组件? [英] Is it ok to register components in Windsor without specifying an interface?

查看:86
本文介绍了是否可以在不指定接口的情况下在Windsor中注册组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不指定接口的情况下在Windsor中注册组件是否被认为是错误的形式?即

Is it considered bad form to register components in Windsor without specifying an interface? i.e.

container.Register(Component.For<MyClass>().LifeStyle.Transient);

而不是...

container.Register(Component.For<IMyClass>().ImplementedBy<MyClass>().LifeStyle.Transient);

我了解对接口进行编码的好处,而不是具体的实现,但是我们发现现在有很多接口,但实际上很多接口都在类上,实际上只有一个实现。

I understand the benefits of coding to an interface rather than a concrete implementation however we are finding that we now have lots of interfaces many of them are on classes that realistically will only ever have one implementation.

推荐答案

是的,没关系可以注册没有界面的组件,但不是出于您给出的原因

Yes, it would be okay to register components without their interfaces, but not for the reason you give.

具体依赖项

可能会发生组件依赖于具体类的情况。例如,使用实体框架,消费者应该将ObjectContext注入其中。这是一个具体的类,仍然需要注入,因为它应该在多个使用者之间共享

It may happen that components depend on concrete classes. For instance, with the Entity Framework consumers should have the ObjectContext injected into them. That's a concrete class that still needs to be injected because it should be shared between several consumers.

因此,给定使用者的构造函数,如下所示:

Thus, given a consumer's constructor like this:

public FooRepository(FooObjectContext objectContext)

您需要像这样配置容器:

you would need to configure the container like this:

container.Register(Component.For<FooObjectContext>());

FooRepository 不需要任何接口,因此注册接口没有任何意义(即使可用),但您仍必须注册具体的类,因为温莎只能仅解析明确注册的类型

The FooRepository requests no interface so it makes no sense registering an interface (even if one was available), but you must still register the concrete class because Windsor can only resolve types explicitly registered.

接口只有一种实现方式

那么只有一种实现方式的接口又如何呢?再次由消费者决定要求。

Then what about interfaces with only one implementation? Once again, the consumer decides the requirements.

想象一下,消费者具有此构造函数:

Imagine that a consumer has this constructor:

public Ploeh(IBar bar)

温莎城堡的唯一出路解决Ploeh是当您注册IBar时。即使Bar是IBar的唯一实现,也不可行

The only way Castle Windsor will be able to resolve Ploeh is when you register IBar. Even if Bar is the only implementation of IBar, this will not work:

container.Register(Component.For<Bar>());

这是行不通的,因为从未注册过IBar。温莎城堡(Castle Windsor)不在乎Bar实现IBar,因为它不想代表您变得聪明。您必须明确告诉它:

This doesn't work because IBar is never registered. Castle Windsor doesn't care that Bar implements IBar because it doesn't want to try to be smart on your behalf. You must tell it explicitly:

container.Register(Component.For<IBar>().ImplementedBy<Bar>());

映射 IBar到Bar。

This maps IBar to Bar.

同时注册接口和具体类型

然后,如果您想同时解析具体类型和

Then what if you want to be able to resolve both the concrete type and the interface?

上一个示例的问题是它可以让您解析IBar,但不能解析Bar。

The problem with the previous example is that it will let you resolve IBar, but not Bar.

您可以使用Forward方法或For的多类重载来转发注册:

You can use the Forward method, or multigeneric overload of For to forward registrations:

container.Register(Component.For<Bar, IBar>().ImplementedBy<Bar>());

这可让您解析 Bar 和IBar。

This lets you resolve both Bar and IBar.

这篇关于是否可以在不指定接口的情况下在Windsor中注册组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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