lexeme
lexeme

Reputation: 2973

Timer events in ASP.NET MVC

Problem: user operates over some entity in a domain. The last one changes its status so that user recieves e-mail notifications (using smtp server) repeatedly until the given time.
So I need to fire an event somehow.

What are the alternative ways to do that? I know there're no events in ASP.NET MVC framework.

Thanks!

Upvotes: 1

Views: 708

Answers (1)

jgauffin
jgauffin

Reputation: 101150

You can use my Inversion Of Control container which has built in support for in-process domain events:

Subscribing

Subscribing is easy. Simply let any class implement IHandlerOf:

[Component]
public class ReplyEmailNotification : IHandlerOf<ReplyPosted>
{
    ISmtpClient _client;
    IUserQueries _userQueries;

    public ReplyEmailNotification(ISmtpClient client, IUserQueries userQueries)
    {
        _client = client;
        _userQueries = userQueries;
    }

    public void Invoke(ReplyPosted e)
    {
        var user = _userQueries.Get(e.PosterId);
        _client.Send(new MailMessage(user.Email, "bla bla"));
    }
} 

Dispatching

Domain events are dispatched using the DomainEvent class. The actual domain event can be any class, there are no restrictions. I do however recommend that you treat them as DTO's.

public class UserCreated
{
    public UserCreated(string id, string displayName)
    {
    }
}

public class UserService
{
    public void Create(string displayName)
    {
        //create user
        // [...]

        // fire the event.
        DomainEvent.Publish(new UserCreated(user.Id, user.DisplayName));
    }
}

The code is from my article: http://www.codeproject.com/Articles/440665/Having-fun-with-Griffin-Container

ASP.NET MVC3 installation:

  1. Use package manager console: install-package griffin.container.mvc3
  2. Follow these instructions: http://griffinframework.net/docs/container/mvc3/

Upvotes: 1

Related Questions