Castle Windsor:有没有办法覆盖组件ID?

时间:2022-01-03 15:48:36

I have the following bit of registration code:

我有以下注册码:

Component.For<IPublishingService>().ImplementedBy<UseStoredProcedureToPrintService>(),
Component.For<IConfirmationDialog<AutomatedTransaction>>().ImplementedBy<ShipmentConfirmationDialog>().Named("ShipmentConfirmationDialog"),
Component.For<IConfirmationService<AutomatedTransaction>>().ImplementedBy<SingleTransactionConfirmation>().ServiceOverrides(
    ServiceOverride.ForKey("shipmentDialog").Eq("ShipmentConfirmationDialog") ),

A requirement came down the line that in some instances the application is supposed to behave somewhat differently. Great I thought, this is exactly what I was using Castle Windsor for to begin with.

在某些情况下,应用程序的行为应该有所不同。伟大的我想,这正是我使用Castle Windsor开始的。

So I wrote my new components and I register them first. For example, I implement IPublishingService differently and register the new implementation first so that it is resolved over the default one above. However, a problem occurrs in the few cases where I had no choice but to use an id to wire up my service overrides. For example how do I redirect the ServiceOverride for ShipmentConfirmationDialog to use my new SpecialCaseShipmentConfirmationDialog implementation without modifying the bit of code above?

所以我写了我的新组件,然后我先注册它们。例如,我以不同方式实现IPublishingService并首先注册新实现,以便通过上面的默认实现解析它。但是,在少数几种情况下我会发现问题,除了使用id来连接我的服务覆盖。例如,如何重定向ShipmentConfirmationDialog的ServiceOverride以使用我的新SpecialCaseShipmentConfirmationDialog实现而不修改上面的代码位?

There are all sorts of mechanisms in castle windsor that I don't really understand like forwarding and ActAs that I'm hoping will provide a simple answer.

在温莎城堡中有各种各样的机制,我不太喜欢转发和ActAs,我希望能提供一个简单的答案。

1 个解决方案

#1


I'd keep it simple. If it's configurable, put it in the config (web.config / app.config) then just load the ID using ConfigurationManager.AppSettings["shipmentDialogToUse"];

我会保持简单。如果它是可配置的,请将其放入配置(web.config / app.config),然后使用ConfigurationManager.AppSettings [“shipmentDialogToUse”]加载ID;

Also remember that the fluent registration API is not the be-all and end-all of registration. XML still has its time and place where it's the right tool for the job.

还要记住,流畅的注册API并不是注册的全部和最终目的。 XML仍然有时间和地点,它是适合工作的正确工具。

#1


I'd keep it simple. If it's configurable, put it in the config (web.config / app.config) then just load the ID using ConfigurationManager.AppSettings["shipmentDialogToUse"];

我会保持简单。如果它是可配置的,请将其放入配置(web.config / app.config),然后使用ConfigurationManager.AppSettings [“shipmentDialogToUse”]加载ID;

Also remember that the fluent registration API is not the be-all and end-all of registration. XML still has its time and place where it's the right tool for the job.

还要记住,流畅的注册API并不是注册的全部和最终目的。 XML仍然有时间和地点,它是适合工作的正确工具。