根据 Ncqrs 的主要贡献者 Pieter 的说法,没有办法开箱即用地做到这一点。
在这种情况下,我不想经历创建和执行命令的整个过程,然后从事件存储中加载聚合根以让它发出事件。
行为是简单的 CRUD,使用最简单的解决方案实现,在这种特定情况下是使用实体框架的表单数据。我唯一需要的是在交易发生后发布一个事件。
我的解决方案如下所示:
// Abstract base class that provides a Unit Of Work
public abstract class EventPublisherMappedByConvention
: AggregateRootMappedByConvention
{
public void Raise(ISourcedEvent e)
{
var context = NcqrsEnvironment.Get<IUnitOfWorkFactory>()
.CreateUnitOfWork(e.EventIdentifier);
ApplyEvent(e);
context.Accept();
}
}
// Concrete implementation for my specific domain
// Note: The events only reflect the CRUD that's happened.
// The methods themselves can stay empty, state has been persisted through
// other means anyway.
public class FleetManagementEventSource : EventPublisherMappedByConvention
{
protected void OnAircraftTypeCreated(AircraftTypeCreated e) { }
protected void OnAircraftTypeUpdated(AircraftTypeUpdated e) { }
// ...
}
// This can be called from anywhere in my application, once the
// EF-based transaction has succeeded:
new FleetManagementEventSource().Raise(new AircraftTypeUpdated { ... });