Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit 38def97

Browse files
Strong typing of Events and Commands is back
It now uses marker interfaces
1 parent 39a1433 commit 38def97

File tree

9 files changed

+431
-421
lines changed

9 files changed

+431
-421
lines changed

CQRSGui/Controllers/HomeController.cs

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
1-
using System;
2-
using System.Web.Mvc;
3-
using SimpleCQRS;
4-
using SimpleCQRS.Commands;
5-
6-
namespace CQRSGui.Controllers
7-
{
8-
[HandleError]
9-
public class HomeController : Controller
10-
{
11-
private FakeBus<object> _bus;
12-
private IReadModelFacade _readmodel;
13-
14-
public HomeController()
15-
{
16-
_bus = ServiceLocator.Bus;
17-
_readmodel = new ReadModelFacade(MvcApplication.Database);
18-
}
19-
20-
public ActionResult Index()
21-
{
22-
ViewData.Model = _readmodel.GetInventoryItems();
23-
24-
return View();
25-
}
26-
27-
public ActionResult Details(Guid id)
28-
{
29-
ViewData.Model = _readmodel.GetInventoryItemDetails(id);
30-
return View();
31-
}
32-
33-
public ActionResult Add()
34-
{
35-
return View();
36-
}
37-
38-
[HttpPost]
39-
public ActionResult Add(string name)
40-
{
41-
_bus.Send(new CreateInventoryItem(Guid.NewGuid(), name));
42-
43-
return RedirectToAction("Index");
44-
}
45-
46-
public ActionResult ChangeName(Guid id)
47-
{
48-
ViewData.Model = _readmodel.GetInventoryItemDetails(id);
49-
return View();
50-
}
51-
52-
[HttpPost]
53-
public ActionResult ChangeName(Guid id, string name, int version)
54-
{
55-
var command = new RenameInventoryItem(id, name, version);
56-
_bus.Send(command);
57-
58-
return RedirectToAction("Index");
59-
}
60-
61-
public ActionResult Deactivate(Guid id, int version)
62-
{
63-
_bus.Send(new DeactivateInventoryItem(id, version));
64-
return RedirectToAction("Index");
65-
}
66-
67-
public ActionResult CheckIn(Guid id)
68-
{
69-
ViewData.Model = _readmodel.GetInventoryItemDetails(id);
70-
return View();
71-
}
72-
73-
[HttpPost]
74-
public ActionResult CheckIn(Guid id, int number, int version)
75-
{
76-
_bus.Send(new CheckInItemsToInventory(id, number, version));
77-
return RedirectToAction("Index");
78-
}
79-
80-
public ActionResult Remove(Guid id)
81-
{
82-
ViewData.Model = _readmodel.GetInventoryItemDetails(id);
83-
return View();
84-
}
85-
86-
[HttpPost]
87-
public ActionResult Remove(Guid id, int number, int version)
88-
{
89-
_bus.Send(new RemoveItemsFromInventory(id, number, version));
90-
return RedirectToAction("Index");
91-
}
92-
}
93-
}
1+
using System;
2+
using System.Web.Mvc;
3+
using SimpleCQRS;
4+
using SimpleCQRS.Commands;
5+
6+
namespace CQRSGui.Controllers
7+
{
8+
[HandleError]
9+
public class HomeController : Controller
10+
{
11+
private FakeBus _bus;
12+
private IReadModelFacade _readmodel;
13+
14+
public HomeController()
15+
{
16+
_bus = ServiceLocator.Bus;
17+
_readmodel = new ReadModelFacade(MvcApplication.Database);
18+
}
19+
20+
public ActionResult Index()
21+
{
22+
ViewData.Model = _readmodel.GetInventoryItems();
23+
24+
return View();
25+
}
26+
27+
public ActionResult Details(Guid id)
28+
{
29+
ViewData.Model = _readmodel.GetInventoryItemDetails(id);
30+
return View();
31+
}
32+
33+
public ActionResult Add()
34+
{
35+
return View();
36+
}
37+
38+
[HttpPost]
39+
public ActionResult Add(string name)
40+
{
41+
_bus.Send(new CreateInventoryItem(Guid.NewGuid(), name));
42+
43+
return RedirectToAction("Index");
44+
}
45+
46+
public ActionResult ChangeName(Guid id)
47+
{
48+
ViewData.Model = _readmodel.GetInventoryItemDetails(id);
49+
return View();
50+
}
51+
52+
[HttpPost]
53+
public ActionResult ChangeName(Guid id, string name, int version)
54+
{
55+
var command = new RenameInventoryItem(id, name, version);
56+
_bus.Send(command);
57+
58+
return RedirectToAction("Index");
59+
}
60+
61+
public ActionResult Deactivate(Guid id, int version)
62+
{
63+
_bus.Send(new DeactivateInventoryItem(id, version));
64+
return RedirectToAction("Index");
65+
}
66+
67+
public ActionResult CheckIn(Guid id)
68+
{
69+
ViewData.Model = _readmodel.GetInventoryItemDetails(id);
70+
return View();
71+
}
72+
73+
[HttpPost]
74+
public ActionResult CheckIn(Guid id, int number, int version)
75+
{
76+
_bus.Send(new CheckInItemsToInventory(id, number, version));
77+
return RedirectToAction("Index");
78+
}
79+
80+
public ActionResult Remove(Guid id)
81+
{
82+
ViewData.Model = _readmodel.GetInventoryItemDetails(id);
83+
return View();
84+
}
85+
86+
[HttpPost]
87+
public ActionResult Remove(Guid id, int number, int version)
88+
{
89+
_bus.Send(new RemoveItemsFromInventory(id, number, version));
90+
return RedirectToAction("Index");
91+
}
92+
}
93+
}

CQRSGui/EventStore.cs

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,80 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using SimpleCQRS;
5-
using SimpleCQRS.Events;
6-
7-
namespace CQRSGui
8-
{
9-
public class EventStore<TEvent> : IEventStore<TEvent>
10-
{
11-
private readonly IEventPublisher<object> _publisher;
12-
13-
private struct EventDescriptor
14-
{
15-
16-
public readonly TEvent EventData;
17-
public readonly Guid Id;
18-
public readonly int Version;
19-
20-
public EventDescriptor(Guid id, TEvent eventData, int version)
21-
{
22-
EventData = eventData;
23-
Version = version;
24-
Id = id;
25-
}
26-
}
27-
28-
public EventStore(IEventPublisher<object> publisher)
29-
{
30-
_publisher = publisher;
31-
}
32-
33-
private readonly Dictionary<Guid, List<EventDescriptor>> _current = new Dictionary<Guid, List<EventDescriptor>>();
34-
35-
public void SaveEvents(Guid aggregateId, int expectedVersion, IEnumerable<TEvent> events)
36-
{
37-
List<EventDescriptor> eventDescriptors;
38-
if(!_current.TryGetValue(aggregateId, out eventDescriptors))
39-
{
40-
eventDescriptors = new List<EventDescriptor>();
41-
_current.Add(aggregateId,eventDescriptors);
42-
}
43-
else if(eventDescriptors[eventDescriptors.Count - 1].Version != expectedVersion && expectedVersion != -1)
44-
{
45-
throw new ConcurrencyException();
46-
}
47-
var i = expectedVersion;
48-
foreach (var @event in events)
49-
{
50-
i++;
51-
52-
eventDescriptors.Add(new EventDescriptor(aggregateId,@event,i));
53-
54-
var metadata = new EventMetadata(i);
55-
56-
_publisher.Publish(@event, metadata);
57-
}
58-
}
59-
60-
public IEnumerable<TEvent> GetEventsForAggregate(Guid aggregateId)
61-
{
62-
List<EventDescriptor> eventDescriptors;
63-
if (!_current.TryGetValue(aggregateId, out eventDescriptors))
64-
{
65-
throw new AggregateNotFoundException();
66-
}
67-
return eventDescriptors.Select(desc => desc.EventData).ToList();
68-
}
69-
70-
}
71-
72-
public class AggregateNotFoundException : Exception
73-
{
74-
}
75-
76-
public class ConcurrencyException : Exception
77-
{
78-
}
79-
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using SimpleCQRS;
5+
using SimpleCQRS.Events;
6+
7+
namespace CQRSGui
8+
{
9+
public class EventStore : IEventStore
10+
{
11+
private readonly IEventPublisher _publisher;
12+
13+
private struct EventDescriptor
14+
{
15+
16+
public readonly Event EventData;
17+
public readonly Guid Id;
18+
public readonly int Version;
19+
20+
public EventDescriptor(Guid id, Event eventData, int version)
21+
{
22+
EventData = eventData;
23+
Version = version;
24+
Id = id;
25+
}
26+
}
27+
28+
public EventStore(IEventPublisher publisher)
29+
{
30+
_publisher = publisher;
31+
}
32+
33+
private readonly Dictionary<Guid, List<EventDescriptor>> _current = new Dictionary<Guid, List<EventDescriptor>>();
34+
35+
public void SaveEvents(Guid aggregateId, int expectedVersion, IEnumerable<Event> events)
36+
{
37+
List<EventDescriptor> eventDescriptors;
38+
if(!_current.TryGetValue(aggregateId, out eventDescriptors))
39+
{
40+
eventDescriptors = new List<EventDescriptor>();
41+
_current.Add(aggregateId,eventDescriptors);
42+
}
43+
else if(eventDescriptors[eventDescriptors.Count - 1].Version != expectedVersion && expectedVersion != -1)
44+
{
45+
throw new ConcurrencyException();
46+
}
47+
var i = expectedVersion;
48+
foreach (var @event in events)
49+
{
50+
i++;
51+
52+
eventDescriptors.Add(new EventDescriptor(aggregateId,@event,i));
53+
54+
var metadata = new EventMetadata(i);
55+
56+
_publisher.Publish(@event, metadata);
57+
}
58+
}
59+
60+
public IEnumerable<Event> GetEventsForAggregate(Guid aggregateId)
61+
{
62+
List<EventDescriptor> eventDescriptors;
63+
if (!_current.TryGetValue(aggregateId, out eventDescriptors))
64+
{
65+
throw new AggregateNotFoundException();
66+
}
67+
return eventDescriptors.Select(desc => desc.EventData).ToList();
68+
}
69+
70+
}
71+
72+
public class AggregateNotFoundException : Exception
73+
{
74+
}
75+
76+
public class ConcurrencyException : Exception
77+
{
78+
}
79+
8080
}

0 commit comments

Comments
 (0)