/
CustomerListView.cs
63 lines (53 loc) · 1.7 KB
/
CustomerListView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using CQRSSample.Domain.Events;
using Raven.Client;
namespace CQRSSample.ReadModel
{
public class CustomerListView : HandlesEvent<CustomerCreatedEvent>, HandlesEvent<CustomerRelocatedEvent>
{
private readonly IDocumentStore _documentStore;
public CustomerListView(IDocumentStore documentStore)
{
_documentStore = documentStore;
}
public void Handle(CustomerRelocatedEvent @event)
{
using (var session = _documentStore.OpenSession())
{
var dto = session.Load<CustomerListDto>(Dto.GetDtoIdOf<CustomerListDto>(@event.AggregateId));
dto.City = @event.City;
session.SaveChanges();
}
}
public void Handle(CustomerCreatedEvent @event)
{
using(var session = _documentStore.OpenSession())
{
var dto = new CustomerListDto { AggregateRootId = @event.AggregateId, City = @event.City, Name = @event.CustomerName };
session.Store(dto);
session.SaveChanges();
}
}
}
public class CustomerListDto : Dto
{
public string City { get; set; }
public string Name { get; set; }
}
public abstract class Dto
{
public string Id
{
get { return GetDtoIdOf(AggregateRootId, GetType()); }
}
public Guid AggregateRootId { get; set; }
public static string GetDtoIdOf<T>(Guid id) where T : Dto
{
return GetDtoIdOf(id, typeof (T));
}
public static string GetDtoIdOf(Guid id, Type type)
{
return type.Name + "/" + id;
}
}
}