ต่อไปเรามาลงมือ Implement Event Store สองสามแบบที่เหมาะกับการใช้งานร่วมกับ A+ES กันจริงๆ เสียที Event Store ที่จะกล่าวถึงในที่นี้ เป็นแบบเรียบง่าย ไม่ได้ออกแบบมาเพื่อประสิทธิภาพสูงสุด แต่ก็ดีพอสำหรับ domain ส่วนใหญ่
แม้การ Implement ของ Event Store แต่ละแบบจะต่างกันออกไป แต่ contracts ของมันจะเหมือนกันเสมอ:
public interface IEventStore
{
// loads all events for a stream
EventStream LoadEventStream(IIdentity id);
// loads subset of events for a stream
EventStream LoadEventStream(
IIdentity id, int skipEvents, int maxCount);
// appends events to a stream, throwing
// OptimisticConcurrencyException another appended
// new events since expectedversion
void AppendToStream(
IIdentity id, int expectedVersion, ICollection<IEvent> events);
}
public class EventStream
{
// version of the event stream returned
public int Version;
// all events in the stream
public IList<IEvent> Events = new List<IEvent>();
}
ดังที่แสดงใน Figure A.14 class ที่ Implement IEventStore คือ wrapper เฉพาะของโปรเจกต์ที่ครอบ IAppendOnlyStore ซึ่งเป็นส่วนที่ generic และนำกลับมาใช้ซ้ำได้มากกว่า ในขณะที่การ Implement IEventStore จะจัดการกับ serialization และ strong typing นั้น การ Implement IAppendOnlyStore กลับให้การเข้าถึง storage engine ต่างๆ ในระดับต่ำ
Figure A.14. ลักษณะของ IEventStore ระดับสูง และ IAppendOnlyStore ระดับต่ำ
Event Store Source Code (ซอร์สโค้ดของ Event Store)
ซอร์สโค้ดฉบับเต็มของ Event Store หลายรูปแบบพร้อมการ Implement storage หลากหลายชนิด สามารถดาวน์โหลดได้ในตัวอย่าง โปรเจกต์ A+ES: http://lokad.github.com/lokad-iddd-sample/ .
ต่อไปนี้คือ interface ของ IAppendOnlyStore ระดับล่าง:
public interface IAppendOnlyStore : IDisposable
{
void Append(string name, byte[] data, int expectedVersion = -1);
IEnumerable<DataWithVersion> ReadRecords(
string name, int afterVersion, int maxCount);
IEnumerable<DataWithName> ReadRecords(
int afterVersion, int maxCount);
void Close();
}
public class DataWithVersion
{
public int Version;
public byte[] Data;
}
public sealed class DataWithName
{
public string Name;
public byte[] Data;
}
อย่างที่เห็น IAppendOnlyStore ทำงานกับ array ของ bytes แทนที่จะเป็น collection ของ event และใช้ string เป็นชื่อแทน identity แบบ strongly typed ที่ระบุชนิดไว้ class EventStore จะทำหน้าที่แปลงข้อมูลระหว่างสองรูปแบบนี้
IAppendOnlyStore ประกาศ method ReadRecords() สองแบบที่แตกต่างกัน method แรกที่แสดงไว้ใช้สำหรับอ่าน Event ใน Stream เดียวโดยระบุชื่อ ส่วน method ที่สองใช้สำหรับอ่าน Event ทั้งหมดใน Store การ Implement method ทั้งสองจะต้องอ่าน Event เรียงตามลำดับที่ถูก persist ไว้เสมอ อย่างที่คุณคงเดาได้ method overload แบบแรกจำเป็นสำหรับการ rebuild สถานะของ Aggregate ตัวเดียว ส่วน ReadRecords() แบบที่สอง infrastructure ใช้เพื่อ replicate Event, publish Event โดยไม่ต้องใช้ two-phase commit และ rebuild persistent read model ที่จำเป็นสำหรับ user interface แบบ CQRS
แนวทางง่ายๆ สำหรับ serialization และ deserialization—การแปลงระหว่าง bytes กับ Event object แบบ strongly typed—ก็คือการใช้ BinaryFormatter ของ .NET:
public class EventStore : IEventStore
{
readonly BinaryFormatter _formatter = new BinaryFormatter();
byte[] SerializeEvent(IEvent[] e)
{
using (var mem = new MemoryStream())
{
_formatter.Serialize(mem, e);
return mem.ToArray();
}
}
IEvent[] DeserializeEvent(byte[] data)
{
using (var mem = new MemoryStream(data))
{
return (IEvent[])_formatter.Deserialize(mem);
}
}
}
ต่อไปนี้คือวิธีที่เราจะใช้ serialization และ deserialization เพื่อโหลด Event Stream:
readonly IAppendOnlyStore _appendOnlyStore;
...
public EventStream LoadEventStream(IIdentity id, int skip, int take)
{
var name = IdentityToString(id);
var records = _appendOnlyStore.ReadRecords(name, skip, take).ToList();
var stream = new EventStream();
foreach (var tapeRecord in records)
{
stream.Events.AddRange(DeserializeEvent(tapeRecord.Data));
stream.Version = tapeRecord.Version;
}
return stream;
}
string IdentityToString(IIdentity id)
{
// in this project all identities produce proper name
return id.ToString();
}
ต่อไปเราจะเห็นวิธี append Event ใหม่ๆ เข้าไปยัง Event Store ผ่านทาง IAppendOnlyStore :
public void AppendToStream(
IIdentity id, int originalVersion, ICollection<IEvent> events)
{
if (events.Count == 0)
return;
var name = IdentityToString(id);
var data = SerializeEvent(events.ToArray());
try
{
_appendOnlyStore.Append(name, data, originalVersion);
}
catch(AppendOnlyStoreConcurrencyException e)
{
// load server events
var server = LoadEventStream(id, 0, int.MaxValue);
// throw a real problem
throw OptimisticConcurrencyException.Create(
server.Version, e.ExpectedVersion, id, server.Events);
}
}