Posted by on Jan 05, 2012 10:43 PM in asp,mvc,twitter,bootstrap,notifications | 0 comments

I have been a long time fan of twitter's bootstrap and always contemplating on ways to create a template that I can use for all my ASP.NET MVC projects.

The one part of the template that i'm currently using is that my services gets injected into my controllers' constructors.

public class UserController : BaseController
{
    private IUserService userService;
    /// <summary>
    /// Initializes a new User Controller
    /// </summary>
    /// <param name="userService"></param>
    public UserController(IUserService userService)
    {
        this.userService = userService;
        this.userService.FailureOccurred += new EventHandler<MessageEventArgs>(base.OnFailureOccurred);
    }
}

I wanted to leverage this and therefor had all my services inherit from a common service that will expose a list of messages as well as raise an event once a failure occurred.

public interface IUserService : IService
{
    /// <summary>
    /// Registers a User
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    bool RegisterUser(User user);
}

By using a simple extension method, we can spit out all the messages at any point in time. For the purpose of the sample project, I call the extension method in the _Layout.cshtml.

alt text

The extension method makes use of bootstrap's good looking alert-message css classes alt text

You can find the sample project here.

Read More