C#实现Event Sourcing遇阻,附简化代码求解决方案
Hey Alex! It sounds like you're deep into building an Event Sourcing system in C# and hit a snag—totally get it, this pattern has some tricky edge cases when you're first getting started. Let's break down what you've shared so far and see where we can help.
First, let's clean up that code snippet you provided to make it easier to work with:
public interface IEvent { } public class UserCreated : IEvent { } public class UserDeleted : IEvent { } public interface IEventSourcable { ICollection<IEvent> History { get; } void ApplyEvent(IEvent ev); // Looks like your original snippet got cut off here! }
It seems your ApplyEvent method definition was truncated—could you share the full signature, plus any implementation code you’ve written for this method, or the specific behavior you’re trying to achieve? A few common pain points that might align with your bottleneck include:
- State Rehydration: Ensuring applying events in order correctly reconstructs your entity’s current state
- Event History Management: Keeping the
Historycollection properly populated and protected from accidental modifications - Type Handling: Issues with deserializing specific event types or routing events to the right handlers
- Method Completeness: Missing parameters or logic in the
ApplyEventmethod that’s preventing it from working as expected
If you can fill in the gaps of where exactly you’re stuck—like error messages, unexpected behavior, or the end goal you’re trying to reach with this code—we can dive into targeted solutions!
内容的提问来源于stack exchange,提问作者Alex B.

