Imagine the following use case: - When a user is created, send a welcome email to the new user's email address.

In the constructor for User, I am publishing an event (pubsub/event aggregator) called UserCreatedEvent. There is a subscriber class that listens for UserCreatedEvent messages, and sends emails to new users.

Now, I only want the email to be sent if the transaction is successfully committed. If I attempt to create a user, yet the commit fails, I don't want an email sent.

Is there a way I can be notified when a particular transaction is successfully committed? For example:

// Constructor for User entity
public User(Session session, string name, string email) : base(session) {
    Name = name;
    Email = email;

    Session.Transaction.Outermost.OnCommit(() => {
        EventAggregator.Publish(new UserCreatedEvent(name, email));
    });
}

asked Mar 09 '12 at 18:21

ara's gravatar image

ara
395838791


2 Answers:

In my solution i have TaskManager and SendMailTask (and other task types). If need to send mail, than i create that task. TaskManager will see the task only after commit.

answered Mar 10 '12 at 03:52

proff's gravatar image

proff
75236

Hello Ara,

Consider subscribing to Session.Events.TransactionCommitted event

answered Mar 10 '12 at 01:34

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA