结构图 ObjectFactory.GetAllInstances>()

发布于 2024-11-08 22:30:43 字数 2641 浏览 0 评论 0 原文

我在最近的项目中实施事件处理时遇到了困难。

我已经验证了结构图正在正确扫描、组装并添加事件处理程序

Scan(cfg =>
            {
               cfg.TheCallingAssembly();
                cfg.IncludeNamespace("ABC.EventHandler");
                cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));

           });

 public class StructureMapEventDispatcher : IEventDispatcher
    {

        public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
        {

            foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
            {

                handler.Handle(eventToDispatch);

            }

        }

    }

在我用来从域触发事件之前, 。像 Dispatcher.RaiseEvent(new [domainEvent class](x,y,z));

这样的事件将会被触发。我必须更改设计,现在我在集合中收集事件

_domainEvents = new Collection<IDomainEvent>();

,然后在将域保存到存储库后引发它,

 public static void Raise(ICollection<IDomainEvent> domainEvents)
        {
            foreach (var domainEvent in domainEvents)
            {
                DomainEventDispatcher.Raise(domainEvent);
            }

        }

但现在

ObjectFactory.GetAllInstances>() 返回 0 处理程序计数

如果我观察

ObjectFactory.GetAllInstances>() 它返回的 正确收集处理程序(目前我有 2 个,它显示 2 个计数)

...我假设这与引发类型 IDomainEvent 而不是实际类型的事件有关,这使得它成为可能结构图很难解决它。

我该如何解决这个问题?

此致,

Mar

-

编辑 1:

我已经确认 structuremap 容器包含从程序集中扫描的所有事件处理程序。

编辑2

我不知道如何让这个问题引起更多关注。我正在为解决方案添加赏金以实现所需的结果。如果问题不清楚,请提问。

基本上我希望 ObjectFactory.GetAllInstances>() 返回 TEvent 的处理程序,其中 TEvent 属于 IDomainEvent 类型。要引发的事件存储在 IDomainEvent 的集合中,并在保存域(从服务层)后引发。

我认为应该有某种方法让结构图知道作为 IDomainEvent 引发的事件实际上是 DomainEvent 类型

var eventsToRaise= Dealer.EventsToRaise (); 从调试窗口添加信息:

在调度程序窗口中引发事件后

在此处输入图像描述

编辑 3: 尽管 eventToRaise 显示为“DealerName Changed”和“DealerCommunicationChanged”
typeof(TEvent) 给出 Type 作为 Domain.IDomainEvent

我猜想是否有可能能够转换为正确的类型(从 VS 监视窗口获取信息的位置)问题可以得到解决

----- 结果 ---

两者方法奏效了。我将这两个问题与团队中的另外 2 名成员进行了接触,我们认为该解决方案未经反思就被选为正确答案。

今天我们将使用更改后的实现进行测试,看看解决方案中的此解决方案是否存在任何问题。

我赞成基于反射的解决方案,因为它也是正确的答案。


I am having a rough time implementing eventing in a recent project.

I have verified that structuremap is scanning properly assemble and adding EventHandlers

Scan(cfg =>
            {
               cfg.TheCallingAssembly();
                cfg.IncludeNamespace("ABC.EventHandler");
                cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));

           });

 public class StructureMapEventDispatcher : IEventDispatcher
    {

        public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
        {

            foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
            {

                handler.Handle(eventToDispatch);

            }

        }

    }

Before I used to fire Event from Domain. Somthing like Dispatcher.RaiseEvent(new [domainEvent class](x,y,z));

and the event will get fired up. I had to change the design where I am now collectiong events in a collection

_domainEvents = new Collection<IDomainEvent>();

and then raising it after I have saved the domain to Repository

 public static void Raise(ICollection<IDomainEvent> domainEvents)
        {
            foreach (var domainEvent in domainEvents)
            {
                DomainEventDispatcher.Raise(domainEvent);
            }

        }

but now

ObjectFactory.GetAllInstances<IHandle<TEvent>>() returns 0 count of handlers

if I watch for

ObjectFactory.GetAllInstances<IHandle<DomainEventClass>>() it returns collection of handlers properly ( currently I have 2 and it shows 2 count)

... I am assuming this has something to do with events being raised as of type IDomainEvent instead of actual type and that is making it hard for structuremap to resolve it.

How can I solve this issue?

Regards,

The Mar

--

Edit 1:

I have conformed that struturemap container contains all event handlers scanned from the assembly.

Edit 2

I dont know how to make this question attract more attention. I am adding bounty for a solution to achieve the results desired. If the question is not clear, please ask.

Basically I want the ObjectFactory.GetAllInstances<IHandle<TEvent>>() to return handlers for TEvent where TEvent is of Type IDomainEvent. Events to be raised are stored in Collection of IDomainEvent and raised after the fact that Domain was saved (from service layer).

I am thinking there should be some way to make structuremap know that the event raised as IDomainEvent is actually of Type DomainEvent

var eventsToRaise= dealer.EventsToRaise();
Adding Information from Debug Window:

After the events have been raised in the dispatcher window

enter image description here

Edit 3:
Eventhough eventToRaise shows as "DealerName Changed" and "DealerCommunicationChanged"
typeof(TEvent) gives Type as Domain.IDomainEvent

I guesss if it is possible to get be able to cast to right type ( from whereever VS watch window is getting info) the problem could get resolved

----- Result---

Both approach worked. I put both approached to 2 other members in my team and we felt that solution without reflection to be selected as right answer.

Today we will be doing a test with changed implementation and see if there are any issues with this solution in the solution.

I have upvoted reflection based solution as it is also right answer.


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难愈 2024-11-15 22:30:43

正如您所说,问题是您正在询问 IHandle 的所有实例的结构映射,但它没有这些实例,结构映射具有具体事件的处理程序。您需要使用事件的实际类型构造类型,然后请求该事件的所有处理程序:

        Type genericHandler = typeof(IHandle<>);
        Type[] typeArgs = { eventToDispatch.GetType() };
        Type neededHandler = genericHandler.MakeGenericType(typeArgs);
        var handlers = ObjectFactory.GetAllInstances(neededHandler);

问题是您最终得到对象的 IList,并且需要将它们转换为正确的处理程序类型,并且它get 有点棘手......一个可能的解决方案是使用反射来调用 Handle() 方法:

        var methodInfo = neededHandler.GetMethod("Handle");
        object[] methodArgs = new object[] { eventToDispatch };
        foreach (var h in handlers)
        {
            methodInfo.Invoke(h, methodArgs);
        }

As you say, the problem is that you're asking structure map for all instances of IHandle<IDomainEvent> and it has none of those, structuremap has handlers for concrete events. You'd need to construct the type using the actual type of the event and then ask for all handlers of that event:

        Type genericHandler = typeof(IHandle<>);
        Type[] typeArgs = { eventToDispatch.GetType() };
        Type neededHandler = genericHandler.MakeGenericType(typeArgs);
        var handlers = ObjectFactory.GetAllInstances(neededHandler);

the problem is that you end up with an IList of objects and you need to cast them to the correct handler type and it get's a bit tricky.... a possible solution is to use reflection to call the Handle() method:

        var methodInfo = neededHandler.GetMethod("Handle");
        object[] methodArgs = new object[] { eventToDispatch };
        foreach (var h in handlers)
        {
            methodInfo.Invoke(h, methodArgs);
        }
单身情人 2024-11-15 22:30:43

我建议您使用记录来保存类型信息,而不是基于反射的方法。像这样的事情:

interface IEventRecord
{
    void Dispatch(IEventDispatcher dispatcher);
}

public class EventRecord<TEvent> : IEventRecord where TEvent : IDomainEvent
{
    TEvent theEvent;

    public EventRecord(TEvent theEvent)
    {
        this.theEvent = theEvent;
    }

    public void Dispatch(IEventDispatcher dispatcher)
    {
        dispatcher.Dispatch(theEvent);
    }
}

如果您发现实例化事件记录很麻烦,则帮助程序可以推断如下所示的类型参数:

public static EventRecord<TEvent> CreateEventRecord<TEvent>(TEvent theEvent) where TEvent : IDomainEvent
{
    return new EventRecord<TEvent>(theEvent);
}

这将允许您实例化如下所示的事件记录:

var record = CreateEventRecord(myDomainEvent);

然后而不是保留 IDomainEvents,保留一个 IEventRecords 集合,其中包含必要的类型数据来引发自身:

foreach (var eventRecord in Records)
{
    eventRecord.Dispatch(myDispatcher);
}

Instead of a reflection-based approach, I would recommend using a record to hold onto the type information for you. Something like this:

interface IEventRecord
{
    void Dispatch(IEventDispatcher dispatcher);
}

public class EventRecord<TEvent> : IEventRecord where TEvent : IDomainEvent
{
    TEvent theEvent;

    public EventRecord(TEvent theEvent)
    {
        this.theEvent = theEvent;
    }

    public void Dispatch(IEventDispatcher dispatcher)
    {
        dispatcher.Dispatch(theEvent);
    }
}

If you find instantiating an event records to be cumbersome, a helper could infer the type parameter like this:

public static EventRecord<TEvent> CreateEventRecord<TEvent>(TEvent theEvent) where TEvent : IDomainEvent
{
    return new EventRecord<TEvent>(theEvent);
}

Which would allow you to instantiate event records like this:

var record = CreateEventRecord(myDomainEvent);

Then instead of holding onto a collection of IDomainEvents, hold onto a collection of IEventRecords that hold the necessary type data to raise themselves:

foreach (var eventRecord in Records)
{
    eventRecord.Dispatch(myDispatcher);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文