0
daniel created
Following the documentation I've injected the EventBus into both my AppServices, but the events are only being triggered in one app service at a time. Never both. If I remove the EventBus from the first App Service, all the events in the second app service will be triggered.
Is this a bug or have I missed something?
Example code:
public class CommentAppService : ApplicationService, ICommentAppService
{
private readonly ICommentRepository _commentRepository;
public IEventBus EventBus { get; set; }
public CommentAppService(ICommentRepository commentRepository)
{
_commentRepository = commentRepository;
EventBus = NullEventBus.Instance;
}
public Comment CreateCommentForTask(CreateCommentInput input) {
var comment = new Comment
{
CommentText = input.CommentText,
TaskId = input.TaskId,
UserId = input.UserId
};
EventBus.Trigger(new TaskCommentCreatedEventData(input.TaskId.Value));
return _commentRepository.Insert(comment);
}
}
public class TaskAppService : ApplicationService, ITaskAppService
{
private readonly IRepository<Task> _taskRepository;
public IEventBus EventBus;
public ProjectTaskActionAppService(IRepository<Task> taskRepository
{
_taskRepository = taskRepository;
EventBus = NullEventBus.Instance;
}
public void UpdateProjectTaskActionStatus(UpdateTaskStatusInput input)
{
var task = _taskRepository.Get(input.TaskId);
task.Completed = input.Completed
if(input.Completed)
{
// TODO: This isn't being triggered!
EventBus.Trigger(new TaskCompleteEventData(task.Id));
}
}
}
public class ActivityLogWriter :
IEventHandler<TaskCommentCreatedEventData>,
IEventHandler<TaskCompleteEventData>,
ITransientDependency
{
private readonly IRepository<ActivityLog> _activityLogRepository;
private readonly IRepository<Task> _taskRepository;
public ActivityLogWriter(
IRepository<ActivityLog> activityLogRepository,
IRepository<ProjectTask> taskRepository
)
{
_activityLogRepository = activityLogRepository;
_taskRepository = taskRepository;
}
/// <summary>
/// Log the event when users comment on a task
/// </summary>
/// <param name="eventData"></param>
public void HandleEvent(TaskCommentCreatedEventData eventData)
{
WriteActivity("{User} commented on the task {Task}", null, eventData.TaskId);
}
/// <summary>
/// Store a log when a user completes a task
/// </summary>
/// <param name="eventData"></param>
public void HandleEvent(TaskCompleteEventData eventData)
{
WriteActivity("{User} complete the the task {Task}", eventData.TaskId);
}
/// <summary>
/// Writes a log to the system
/// </summary>
/// <param name="message">The message to write as a long</param>
/// <param name="projectTaskId">The project task which the event related to.</param>
public void WriteActivity(string message, taskId)
{
_activityLogRepository.Insert(new ActivityLog
{
Message = message,
TaskId = taskId
});
}
}
1 Answer(s)
-
0
Hi,
In the second application service (TaskAppService), EventBus defined wrongly. It should be like:
public IEventBus EventBus { get; set; }
NOT like:
public IEventBus EventBus;
If you define it as above, Dependency Injection can not set it, and it remains NullEventBus.Instance.