Permalink
Browse files

Created own dto for customer relocated view

  • Loading branch information...
1 parent ef01dac commit 0d2baa45bafef92b8c472b73d66213ef6af7364f JoergEg committed Mar 14, 2011
@@ -63,7 +63,7 @@ private static void ShowCustomerListView(IDocumentStore store)
{
foreach(var dto in session.Query<CustomerListDto>())
{
- Console.WriteLine(dto.Name + " now living in " + dto.City + " (" + dto.Id + ")");
+ Console.WriteLine(dto.Name + " now living in " + dto.City + " (" + dto.AggregateRootId + ")");
Console.WriteLine("---");
}
}
@@ -32,9 +32,15 @@ public static IWindsorContainer BootStrap(IDocumentStore store)
private static void SetupDomainEventHandlers(IBus bus, IDocumentStore documentStore)
{
+ //TODO: Resolve through IoC
+
var view = new CustomerListView(documentStore);
bus.RegisterHandler<CustomerCreatedEvent>(view.Handle);
bus.RegisterHandler<CustomerRelocatedEvent>(view.Handle);
+
+ var addressView = new CustomerAddressView(documentStore);
+ bus.RegisterHandler<CustomerCreatedEvent>(addressView.Handle);
+ bus.RegisterHandler<CustomerRelocatedEvent>(addressView.Handle);
}
}
@@ -37,6 +37,7 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="CustomerAddressView.cs" />
<Compile Include="CustomerListView.cs" />
<Compile Include="HandlesEvent.cs" />
<Compile Include="IReadRepository.cs" />
@@ -0,0 +1,52 @@
+using System;
+using CQRSSample.Domain.Events;
+using Raven.Client;
+
+namespace CQRSSample.ReadModel
+{
+ public class CustomerAddressView : HandlesEvent<CustomerCreatedEvent>, HandlesEvent<CustomerRelocatedEvent>
+ {
+ private readonly IDocumentStore _documentStore;
+
+ public CustomerAddressView(IDocumentStore documentStore)
+ {
+ _documentStore = documentStore;
+ }
+
+ public void Handle(CustomerRelocatedEvent @event)
+ {
+ using (var session = _documentStore.OpenSession())
+ {
+ var dto = session.Load<CustomerAddressDto>(Dto.GetDtoIdOf<CustomerAddressDto>(@event.AggregateId));
+ dto.Street = @event.Street;
+ dto.StreetNumber = @event.StreetNumber;
+ dto.PostalCode = @event.PostalCode;
+ dto.City = @event.City;
+ session.SaveChanges();
+ }
+ }
+
+ public void Handle(CustomerCreatedEvent @event)
+ {
+ using(var session = _documentStore.OpenSession())
+ {
+ var dto = new CustomerAddressDto { AggregateRootId = @event.AggregateId, CustomerName = @event.CustomerName, Street = @event.Street, StreetNumber = @event.StreetNumber, PostalCode = @event.PostalCode, City = @event.City };
+ session.Store(dto);
+ session.SaveChanges();
+ }
+ }
+ }
+
+ public class CustomerAddressDto : Dto
+ {
+ public string City { get; set; }
+
+ public string PostalCode { get; set; }
+
+ public string StreetNumber { get; set; }
+
+ public string Street { get; set; }
+
+ public string CustomerName { get; set; }
+ }
+}
@@ -17,7 +17,7 @@ public void Handle(CustomerRelocatedEvent @event)
{
using (var session = _documentStore.OpenSession())
{
- var dto = session.Load<CustomerListDto>(@event.AggregateId.ToString());
+ var dto = session.Load<CustomerListDto>(Dto.GetDtoIdOf<CustomerListDto>(@event.AggregateId));
dto.City = @event.City;
session.SaveChanges();
}
@@ -27,19 +27,37 @@ public void Handle(CustomerCreatedEvent @event)
{
using(var session = _documentStore.OpenSession())
{
- var dto = new CustomerListDto { Id = @event.AggregateId, City = @event.City, Name = @event.CustomerName };
+ var dto = new CustomerListDto { AggregateRootId = @event.AggregateId, City = @event.City, Name = @event.CustomerName };
session.Store(dto);
session.SaveChanges();
}
}
}
- public class CustomerListDto
+ public class CustomerListDto : Dto
{
- public Guid Id { get; set; }
-
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;
+ }
+ }
}
@@ -7,7 +7,7 @@ namespace CQRSSample.ReadModel
public interface IReadRepository
{
IQueryable<T> GetAll<T>() where T : class;
- T GetById<T>(Guid id) where T : class;
+ T GetById<T>(string id) where T : class;
}
public class RavenReadRepository : IReadRepository
@@ -27,11 +27,11 @@ public RavenReadRepository(IDocumentStore documentStore)
}
}
- public T GetById<T>(Guid id) where T : class
+ public T GetById<T>(string id) where T : class
{
using (var session = _documentStore.OpenSession())
{
- return session.Load<T>(id.ToString());
+ return session.Load<T>(id);
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -3,5 +3,6 @@
xmlns:Converters="clr-namespace:CQRSSample.WpfClient.ApplicationFramework.Converters">
<Converters:CollapsedWhenNullConverter x:Key="collapsedWhenNullConverter" />
+ <BooleanToVisibilityConverter x:Key="booleanToVisibility" />
</ResourceDictionary>
@@ -99,6 +99,7 @@
<DependentUpon>CustomerRelocatingView.xaml</DependentUpon>
</Compile>
<Compile Include="Modules\CustomerDetails\CustomerRelocating\CustomerRelocatingViewModel.cs" />
+ <Compile Include="Modules\CustomerDetails\IShowCustomerDetails.cs" />
<Compile Include="Modules\CustomerDetails\WhatsNext\WhatsNextView.xaml.cs">
<DependentUpon>WhatsNextView.xaml</DependentUpon>
</Compile>
@@ -143,10 +144,6 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
- <Page Include="ApplicationFramework\Resources\Style.xaml">
- <SubType>Designer</SubType>
- <Generator>MSBuild:Compile</Generator>
- </Page>
<Page Include="Modules\CustomerDetails\CreateCustomer\CreateCustomerView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -228,6 +225,9 @@
<Resource Include="ApplicationFramework\Icons\customer.png" />
<Resource Include="ApplicationFramework\Icons\no.png" />
</ItemGroup>
+ <ItemGroup>
+ <Resource Include="ApplicationFramework\Icons\relocate.png" />
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -8,12 +8,11 @@
<StackPanel HorizontalAlignment="Center">
<StackPanel VerticalAlignment="Center" Margin="0 0 0 20">
<TextBlock FontSize="40" FontWeight="ExtraLight" Text="{Binding ViewModel.Name}" />
- <TextBlock HorizontalAlignment="Right" FontWeight="ExtraLight">All you want to know about your cusomter</TextBlock>
+ <TextBlock HorizontalAlignment="Right" FontWeight="ExtraLight">All you want to know about your customer</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="0 0 2 0">Now living in</TextBlock>
<TextBlock Text="{Binding ViewModel.City}"></TextBlock>
</StackPanel>
- <Button x:Name="RelocateCustomer" Width="200" Margin="0 20 0 0">Customer relocated</Button>
</StackPanel>
</UserControl>
@@ -4,38 +4,26 @@
namespace CQRSSample.WpfClient.Modules.CustomerDetails.CustomerDetailsOverview
{
- public class CustomerDetailsOverviewViewModel : Screen
+ public class CustomerDetailsOverviewViewModel : Screen, IShowCustomerDetails
{
private readonly IReadRepository _readRepository;
- private readonly IEventAggregator _eventAggregator;
- public CustomerDetailsOverviewViewModel(IReadRepository readRepository, IEventAggregator eventAggregator)
+ public CustomerDetailsOverviewViewModel(IReadRepository readRepository)
{
_readRepository = readRepository;
- _eventAggregator = eventAggregator;
}
- public void WithCustomer(Guid customerId)
+ public void WithCustomer(string customerDtoId)
{
- ViewModel = _readRepository.GetById<CustomerListDto>(customerId);
+ ViewModel = _readRepository.GetById<CustomerListDto>(customerDtoId);
}
- //TODO: Change CustomerListDto to something specific for this screen
- public CustomerListDto ViewModel { get; private set; }
-
- public void RelocateCustomer()
+ public Guid GetCustomerId()
{
- _eventAggregator.Publish(new ShowCustomerRelocatingEvent(ViewModel.Id));
+ return ViewModel.AggregateRootId;
}
- }
-
- public class ShowCustomerRelocatingEvent
- {
- public readonly Guid CustomerId;
- public ShowCustomerRelocatingEvent(Guid customerId)
- {
- CustomerId = customerId;
- }
+ //TODO: Change CustomerListDto to something specific for this screen
+ public CustomerListDto ViewModel { get; private set; }
}
}
@@ -6,6 +6,9 @@
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="600">
<StackPanel>
+ <TextBlock>Address:</TextBlock>
+ <TextBlock x:Name="Address"></TextBlock>
+ <Separator />
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Margin="0 0 10 0">Street:</TextBlock>
<TextBox Text="{Binding Command.Street}" Width="400" />
@@ -19,16 +19,25 @@ public CustomerRelocatingViewModel(IBus bus, IEventAggregator eventAggregator, I
_eventAggregator = eventAggregator;
_readRepository = readRepository;
+ //TODO: Resolve through IoC Container
Validator = new RelocatingCustomerValidator();
}
public void WithCustomer(Guid customerId)
{
- ViewModel = _readRepository.GetById<CustomerListDto>(customerId);
- Command = new ValidatingCommand<RelocatingCustomerCommand>(new RelocatingCustomerCommand(ViewModel.Id), Validator);
+ ViewModel = _readRepository.GetById<CustomerAddressDto>(Dto.GetDtoIdOf<CustomerAddressDto>(customerId));
+ Command = new ValidatingCommand<RelocatingCustomerCommand>(new RelocatingCustomerCommand(ViewModel.AggregateRootId), Validator);
}
- public CustomerListDto ViewModel { get; private set; }
+ public CustomerAddressDto ViewModel { get; private set; }
+
+ public string Address
+ {
+ get
+ {
+ return string.Format("{0} {1}, {2} {3}", ViewModel.Street, ViewModel.StreetNumber, ViewModel.PostalCode, ViewModel.City);
+ }
+ }
public void Save()
{
@@ -0,0 +1,11 @@
+using System;
+
+namespace CQRSSample.WpfClient.Modules.CustomerDetails
+{
+ public interface IShowCustomerDetails
+ {
+ void WithCustomer(string customerId);
+
+ Guid GetCustomerId();
+ }
+}
@@ -59,11 +59,11 @@ public void ShowCustomerDetails(CustomerListDto dto)
public class ShowCustomerDetailsEvent
{
- public readonly Guid Id;
+ public readonly string DtoId;
- public ShowCustomerDetailsEvent(Guid id)
+ public ShowCustomerDetailsEvent(string dtoId)
{
- Id = id;
+ DtoId = dtoId;
}
}
}
@@ -22,6 +22,7 @@
</ribbon:RibbonGroup>
<ribbon:RibbonGroup Header="Customer Details">
<ribbon:RibbonButton x:Name="AddNewCustomer" LargeImageSource="..\..\ApplicationFramework\Icons\customer.png" Label="Add new customer" />
+ <ribbon:RibbonButton x:Name="RelocateCustomer" Visibility="{Binding ShowCustomerDetailButtons, Converter={StaticResource booleanToVisibility}}" LargeImageSource="..\..\ApplicationFramework\Icons\relocate.png" Label="Customer relocated" />
</ribbon:RibbonGroup>
</ribbon:RibbonTab>
Oops, something went wrong.

0 comments on commit 0d2baa4

Please sign in to comment.