Files
event-sourcing-jambo/source/Consumer/Jambo.Consumer.Application/DomainEventHandlers/Posts/PostPublishedDomainEventHandler.cs
Ivan Paulovich fc74b10766 Spliting projects
2017-12-21 15:59:03 -02:00

35 lines
1.3 KiB
C#

using Jambo.Domain.Exceptions;
using Jambo.Domain.Model.Posts;
using Jambo.Domain.Model.Posts.Events;
using MediatR;
using System;
namespace Jambo.Consumer.Application.DomainEventHandlers.Posts
{
public class PostPublishedDomainEventHandler : IRequestHandler<PostPublishedDomainEvent>
{
private readonly IPostReadOnlyRepository postReadOnlyRepository;
private readonly IPostWriteOnlyRepository postWriteOnlyRepository;
public PostPublishedDomainEventHandler(
IPostReadOnlyRepository postReadOnlyRepository,
IPostWriteOnlyRepository postWriteOnlyRepository)
{
this.postReadOnlyRepository = postReadOnlyRepository ??
throw new ArgumentNullException(nameof(postReadOnlyRepository));
this.postWriteOnlyRepository = postWriteOnlyRepository ??
throw new ArgumentNullException(nameof(postWriteOnlyRepository));
}
public void Handle(PostPublishedDomainEvent domainEvent)
{
Post post = postReadOnlyRepository.GetPost(domainEvent.AggregateRootId).Result;
if (post.Version != domainEvent.Version)
throw new TransactionConflictException(post, domainEvent);
post.Apply(domainEvent);
postWriteOnlyRepository.UpdatePost(post).Wait();
}
}
}