Created
March 13, 2015 18:55
-
-
Save ruslander/99b1bf4ef48e26ef7a7d to your computer and use it in GitHub Desktop.
Dist es
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EsAr | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var s0 = new List<object>() | |
{ | |
new UserCreated("1000"), | |
new UserCreated("1001") | |
}; | |
var v0 = new UniqueUsersAuthoritySaga(s0); | |
v0.NewUser("1002"); | |
var s1 = new List<object>(s0); | |
s1.AddRange(v0.UncommittedEvents); | |
} | |
} | |
public class Saga | |
{ | |
public readonly List<object> UncommittedEvents = new List<object>(); | |
protected void Apply(object evt) | |
{ | |
// are you master | |
UncommittedEvents.Add(evt); | |
((dynamic)this).When((dynamic)evt); | |
} | |
public Saga(List<object> evts) | |
{ | |
foreach (var evt in evts) | |
((dynamic) this).When((dynamic) evt); | |
} | |
} | |
public class UniqueUsersAuthoritySaga : Saga | |
{ | |
public readonly List<string> Users = new List<string>(); | |
public UniqueUsersAuthoritySaga(List<object> evts) : base(evts){} | |
public void NewUser(string name) | |
{ | |
if(Users.Contains(name)) | |
throw new Exception(name + " alreadey exists"); | |
Apply(new UserCreated(name)); | |
} | |
public void When(UserCreated evt) | |
{ | |
Users.Add(evt.Name); | |
} | |
} | |
public class UserCreated | |
{ | |
public string Name { get; set; } | |
public UserCreated(string name) | |
{ | |
Name = name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment