Created
June 10, 2014 18:08
-
-
Save hagbarddenstore/003b045fda0fe06d266d to your computer and use it in GitHub Desktop.
This file contains 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
interface EventStream | |
{ | |
// Just a marker interface to let you know which classes are persisted. | |
} | |
class Site : EventStream | |
{ | |
private readonly HashSet<string> _registeredUsers = new HashSet<string>(); | |
public string Id { get; private set; } | |
public User RegisterUser(string username) | |
{ | |
if (_registeredUsers.Contains(username.ToLower())) | |
{ | |
throw new NonUniqueUsernameException(username); | |
} | |
var user = new User(username); | |
_registeredUsers.Add(username.ToLower()); | |
return user; | |
} | |
} | |
class User : EventStream | |
{ | |
public User(string username) | |
{ | |
Username = username; | |
} | |
public int Id { get; private set; } | |
public string Username { get; private set; } | |
} | |
// From UI | |
class UsersController : Controller | |
{ | |
public ActionResult Index(int pageIndex = 1) | |
{ | |
// Read model backed by a database, file, in-memory, etc. | |
var registeredUsersView = new RegisteredUsersView(); | |
var users = registeredUsersView.FindAll(pageIndex); | |
return View(users); | |
} | |
public ActionResult Create() | |
{ | |
return View(); | |
} | |
[HttpPost] | |
public ActionResult Create(string username) | |
{ | |
// Magic class, depends on implementation? | |
var eventStore = new EventStore(); | |
var site = eventStore.Get<Site>("MySite"); | |
try | |
{ | |
var user = site.CreateUser(username); | |
eventStore.Persist(site); | |
eventStore.Persist(user); | |
/** | |
* TODO: Either publish the events somehow, or let the event store | |
* publish the events, or execute the "update view logic" right away | |
* when the unit of work is disposed. | |
*/ | |
return RedirectToAction("Index"); | |
} | |
catch (NonUniqueUsernameException) | |
{ | |
// TODO: Add errors... | |
return View(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment