forked from JoergEg/CQRS-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerRelocatingViewModel.cs
50 lines (40 loc) · 1.52 KB
/
CustomerRelocatingViewModel.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
using System;
using Caliburn.Micro;
using CQRSSample.Commands;
using CQRSSample.Infrastructure;
using CQRSSample.ReadModel;
using CQRSSample.WpfClient.ApplicationFramework;
namespace CQRSSample.WpfClient.Modules.CustomerDetails.CustomerRelocating
{
public class CustomerRelocatingViewModel : ScreenWithValidatingCommand<RelocatingCustomerCommand>
{
private readonly IBus _bus;
private readonly IEventAggregator _eventAggregator;
private readonly IReadRepository _readRepository;
public CustomerRelocatingViewModel(IBus bus, IEventAggregator eventAggregator, IReadRepository readRepository)
{
_bus = bus;
_eventAggregator = eventAggregator;
_readRepository = readRepository;
Validator = new RelocatingCustomerValidator();
}
public void WithCustomer(Guid customerId)
{
ViewModel = _readRepository.GetById<CustomerListDto>(customerId);
Command = new ValidatingCommand<RelocatingCustomerCommand>(new RelocatingCustomerCommand(ViewModel.Id), Validator);
}
public CustomerListDto ViewModel { get; private set; }
public void Save()
{
if(!Validate().IsValid)
return;
//important: send command over bus
_bus.Send(Command.InnerCommand);
//signal for UI - change view
_eventAggregator.Publish(new CustomerRelocatingSavedEvent());
}
}
public class CustomerRelocatingSavedEvent
{
}
}