Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View Context
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
readonly ICollection<Action> _transactionCallbacks = new Collection<Action>();
 
public override int SaveChanges() {
int r;
try {
ExecuteDomainEvents();
r = base.SaveChanges();
} catch (Exception) {
_transactionCallbacks.Clear();
throw;
}
ExecuteTransactionCallbacks();
return r;
}
 
public override async Task<int> SaveChangesAsync() {
int r;
try {
ExecuteDomainEvents();
r = await base.SaveChangesAsync().ConfigureAwait(false);
} catch (Exception) {
_transactionCallbacks.Clear();
throw;
}
ExecuteTransactionCallbacks();
return r;
}
 
public override async Task<int> SaveChangesAsync(CancellationToken cts) {
int r;
try {
ExecuteDomainEvents();
r = await base.SaveChangesAsync(cts).ConfigureAwait(false);
} catch (Exception) {
_transactionCallbacks.Clear();
throw;
}
ExecuteTransactionCallbacks();
return r;
}
 
void ExecuteDomainEvents() {
foreach (var entity in GetEntitiesWithPendingEvents())
ExecuteEntityEvents(entity);
}
 
IEntity[] GetEntitiesWithPendingEvents() {
return ChangeTracker.Entries<IEntity>()
.Select(po => po.Entity)
.Where(po => po.Events.Any())
.ToArray();
}
 
void ExecuteEntityEvents(IEntity entity) {
var events = entity.Events.ToArray();
entity.Events.Clear();
 
foreach (var domainEvent in events)
_mediator.Notify(domainEvent);
}
 
public void AddTransactionCallback(Action act) {
_transactionCallbacks.Add(act);
}
 
void ExecuteTransactionCallbacks() {
var callbacks = _transactionCallbacks.ToArray();
_transactionCallbacks.Clear();
 
foreach (var c in callbacks)
c();
}
View Context
1 2 3 4 5 6 7 8
public void AddChatMessage(string message, Account account) {
Contract.Requires<ArgumentNullException>(message != null);
Contract.Requires<ArgumentNullException>(account != null);
 
var chatHubMessage = new ChatHubMessage(message, account);
ChatMessages.Add(chatHubMessage);
Events.Add(new ChatHubMessageCreated(this, chatHubMessage));
}
View Context
1 2 3 4 5 6 7 8 9 10 11 12
public class SendChatMessageToSignalR : SignalRBaseHandler<IChatMessenger>,
INotificationHandler<ChatHubMessageCreated>
{
public SendChatMessageToSignalR(ISqlContext context, IChatMessenger messenger) : base(context, messenger) {}
 
public void Handle(ChatHubMessageCreated notification) {
Context.AddTransactionCallback(
() =>
Messenger.SendChatMessage(notification.ChatHub.Uuid,
notification.ChatHubMessage.Map<ChatMessageModel>()));
}
}
View Context
1 2 3 4 5 6 7 8 9 10 11 12 13
public class ChatHubMessageCreated : IDomainEvent
{
public ChatHubMessageCreated(ChatHub chatHub, ChatHubMessage chatHubMessage) {
Contract.Requires<ArgumentNullException>(chatHub != null);
Contract.Requires<ArgumentNullException>(chatHubMessage != null);
 
ChatHub = chatHub;
ChatHubMessage = chatHubMessage;
}
 
public ChatHub ChatHub { get; private set; }
public ChatHubMessage ChatHubMessage { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.