0

I've read https://aspnetboilerplate.com/Pages/Documents/EventBus-Domain-Events and also ABP's implementation of Entity event handlers https://github.com/aspnetboilerplate/aspnetboilerplate/tree/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Events/Bus/Entities

I have spent 8 hours at work trying to find a solution to my issue, but I failed to succeed.

I have certain entities that point to a single entity called DatumStatus, which records certain actions that generate different states, such as: approved, modified, reviewed, archived, etc.

I am trying to generate a generic EventHandler capable of modifying its status based on these actions.

An example based on a algorithm:

EventBus.Trigger(new ApproveEventData{
    Repository = _certainRepository,
    Ids = [1, 4, 5]
});

The handler itself would, in turn, handle this state transition

public void HandleEvent(ApproveEventData eventData)
{
    eventData.Repository.Where(p => p.Id.IsIn(eventData.Ids)).ForEach(p => {
        p.Approved = true;
        p.ApprovalDate = DateTime.Now()
    });
}

The problem is, I need to write a generic ApproveEventData and handler capable of firing the same HandleEvent for every single entities.

The "closest" I got is:

EventBus.Trigger(typeof(ApproveEventData<int>), (IEventData) new ApproveEventData<int> {
    Repository = (IRepository<EntityWithStatus<int>, int>) _entityRepository,
    Ids = selectedIds
});

[Serializable]
public class ApproveEventData<TPrimaryKey> : EventData
{
    public IRepository<EntityWithStatus<TPrimaryKey>, TPrimaryKey> Repository;
    public TPrimaryKey[] Ids;
}

The implementation above failes when casting the repository.

Could someone shed some light? Thanks!

4

1 回答 1

2

我看到了两种可能的方法。

  1. 依赖协变和逆变。您可以通过使 EntityWithStatus 的接口成为接口并使 IEntityWithStatus 和 IRepository 协变(添加到泛型类型定义)来使转换成功。

  2. 依赖动态并利用泛型类型推断。基本上让存储库是动态的。

我推荐1号。

于 2017-08-21T19:36:00.167 回答