gpt4 book ai didi

dependency-injection - 如何使用 StructureMap 为静态类指定 setter 注入(inject)?

转载 作者:行者123 更新时间:2023-12-04 08:20:39 25 4
gpt4 key购买 nike

我目前正在研究设计模式书中的这段代码:

public static class DomainEvents
{
public static IDomainEventHandlerFactory DomainEventHandlerFactory { get; set; }

public static void Raise<T>(T domainEvent) where T : IDomainEvent
{
DomainEventHandlerFactory.GetDomainEventHandlersFor(domainEvent).ForEach(h => h.Handle(domainEvent));
}
}

这涉及连接 DomainEvents,这个特定的代码片段负责允许我通过 DomainEvents 上的 Raise 方法引发事件。

这是我的 Bootstrapper 文件中的代码:

public class ControllerRegistry : Registry
{
public ControllerRegistry()
{
ForRequestedType<IDomainEventHandlerFactory>().TheDefault.Is.OfConcreteType<StructureMapDomainEventHandlerFactory>();
ForRequestedType<IDomainEventHandler<OrderSubmittedEvent>>().AddConcreteType<OrderSubmittedHandler>();
}
}

当我从服务层访问 DomainEvents.Raise 时(在本例中,我正在引发 OrderSumittedEvent),DomainEventsnull(当它应该通过 StructureMap 设置时):

public class OrderService
{
public void Create(Order order)
{
DomainEvents.Raise(new OrderSubmittedEvent() { Order = order });
}
}

除非我手动将 DomainEvents.DomainEventHandlerFactory 显式设置为 StructureMapDomainEventHandlerFactory,如下所示:

public class OrderService
{
public void Create(Order order)
{
// this is the manual assignment I have to make into the DomainEventHandlerFactory of
// the static DomainEvents class. Basically, this is what StructureMap should take care
// of for me, but is not.
DomainEvents.DomainEventHandlerFactory = new StructureMapDomainEventHandlerFactory();

DomainEvents.Raise(new OrderSubmittedEvent() { Order = order });
}
}

下面是使用 .WhatDoIHave() 的 StrucutureMap 的输出,看起来 StructureMap 确实有 StructureMapDomainEventHandlerFactory 类型的配置实例 IDomainEventHandlerFactory。这是转储:

================================================================================================================================================================================================================================================================================================================================================================================================
PluginType Name Description
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Func`1<TResult> (Func`1<TResult>)
Scoped as: Transient

311731d7-60c7-46de-9ef4-24608f21df04
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IDomainEventHandlerFactory (DE.Infrastructure.Domain.Events.IDomainEventHandlerFactory) dbcb010c-fa92-4137-85b8-161ab17c2c98 Configured Instance of DE.Infrastructure.Domain.Events.StructureMapDomainEventHandlerFactory, DE.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Scoped as: Transient

dbcb010c-fa92-4137-85b8-161ab17c2c98 Configured Instance of DE.Infrastructure.Domain.Events.StructureMapDomainEventHandlerFactory, DE.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IDomainEventHandler`1<OrderSubmittedEvent> (IDomainEventHandler`1<OrderSubmittedEvent>) DE.Services.DomainEventHandlers.OrderSubmittedHandler, DE.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Configured Instance of DE.Services.DomainEventHandlers.OrderSubmittedHandler, DE.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Scoped as: Transient

DE.Services.DomainEventHandlers.OrderSubmittedHandler, DE.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Configured Instance of DE.Services.DomainEventHandlers.OrderSubmittedHandler, DE.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IContainer (StructureMap.IContainer) e24f9e45-caaf-48b6-89f7-d8125310102a Object: StructureMap.Container
Scoped as: Transient

e24f9e45-caaf-48b6-89f7-d8125310102a Object: StructureMap.Container
================================================================================================================================================================================================================================================================================================================================================================================================

我使用过 StructureMap,但我没有使用 Setter 注入(inject),我也不必处理将 StructureMap 与静态类一起使用(如果这有意义的话),所以我有点迷茫为什么要使用这段代码行不通。

是否可以将 Setter 注入(inject)与这样的类的静态实现一起使用?我没有正确使用 StructureMap 吗?StructureMap 是否应该负责将 DomainEvents 类创建为单例,并且我可以摆脱静态实现?

谢谢,迈克

最佳答案

Is it possible to use Setter Injection with a static implementation of a class like this?

没有。只有可创建的类才有可能。

Am I not using StructureMap properly?

你不是。依赖注入(inject)处理创建和提供实例。根据定义,静态类在代码中不可创建,因此不可注入(inject)。

Should StructureMap be responsible for creating the DomainEvents class as a Singleton, and I can get rid of the static implementation?

看起来这种设计的核心特征是使用单个实例,因此事件只触发一次。我不确定你想要什么设计模式,但在很多情况下你可以用 Action<T> 代替或 Func<T>对于事件,如果您正确使用单例生活方式,您可能可以注册一个可以在所有类之间共享的实例,而不是求助于静态类。

如果绝对需要静态事件,您可以在您的应用程序启动代码中注入(inject)一个 IDomainEventHandlerFactory 实例,方法是在您完成注册代码(在您的 composition root 中)后手动解析并设置它。

// Provide the DI container with configuration
var container = DIConfig();

// Set the static event
DomainEvents.DomainEventHandlerFactory = container.Resolve<IDomainEventHandlerFactory>();

为确保您的应用程序不会通过多次调用或在设置之前使用它来滥用 setter ,您可以在静态类中放置一些保护子句。

public static class DomainEvents
{
private static IDomainEventHandlerFactory domainEventHandlerFactory;

public static IDomainEventHandlerFactory DomainEventHandlerFactory
{
get
{
return domainEventHandlerFactory;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
if (domainEventHandlerFactory != null)
throw new ArgumentException("DomainEventHandlerFactory is already set and cannot be set again.");
domainEventHandlerFactory = value;
}
}

public static void Raise<T>(T domainEvent) where T : IDomainEvent
{
ThrowIfNotInitialized()
DomainEventHandlerFactory.GetDomainEventHandlersFor(domainEvent).ForEach(h => h.Handle(domainEvent));
}

private static void ThrowIfNotInitialized()
{
if (domainEventHandlerFactory == null)
{
throw new MvcSiteMapException("Not initialized. You must set DomainEventHandlerFactory at application start.");
}
}
}

关于dependency-injection - 如何使用 StructureMap 为静态类指定 setter 注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8645848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com