Image Image Image Image Image
Scroll to Top

To Top

AOP Aspect-oriented programming (AOP) is a pragmatic passion that I document here.

Unity Application Block: Event Broker with ASP.NET MVC

On 03, Dez 2010 | inAOP, Clean Code Developer | vonJohannes Hoppe

For my colleagues at work I evaluated the Simple Event Broker from the Patterns and Practices team. In my opinion this extension is a very valuable piece of software!

To learn more about the Event Broker, you should read:

I made a small sample solution, based on my “WebNoteMvc” project.

The scenario is easy: I want my repositories (or services) to be loosely coupled. In my database foreign references are used. So before I delete data from one repository other repositories should get a notice. The related repositories are now forced to delete their own related data from the DB to keep the data integrity.

My setup of the UnityContainer looks like this:

private static IUnityContainer ContainerSetup()
{
    IUnityContainer container = new UnityContainer()
 
        .AddNewExtension<SimpleEventBrokerExtension>()
 
        .RegisterType<IWebNote, WebNote>()
        .RegisterTypeForEventBroker<IWebNoteRepository, WebNoteRepository>()
 
        .RegisterType<IWebNoteCategory, WebNoteCategory>()
        .RegisterTypeForEventBroker<IWebNoteCategoryRepository, WebNoteCategoryRepository>();
 
    return container;
}

I introduced a small Extension method called “RegisterTypeForEventBroker“, to make sure that the observers are at least once resolved before the first event. Warning: Your events could be silently ignored, if you forget that extra step! It’s also a good idea to use the repository as a Singleton. (infinite lifetime)

namespace WebNoteMvc.Code.Unity
{
    using Microsoft.Practices.Unity;
    using Singleton = Microsoft.Practices.Unity.ContainerControlledLifetimeManager;
 
    /// <summary>
    /// Extention method for IUnityContainer
    /// </summary>
    public static class UnityContainerExtensions
    {
        /// <summary>
        /// Register a type mapping that can be used for the Event Broker Extension
        /// </summary>
        /// <typeparam name="TFrom">System.Type that will be requested.</typeparam>
        /// <typeparam name="TTo">System.Type that will actually be returned.</typeparam>
        /// <param name="container">Container to configure.</param>
        /// <returns>The Microsoft.Practices.Unity.UnityContainer object that this method was called on</returns>
        public static IUnityContainer RegisterTypeForEventBroker<TFrom, TTo>(this IUnityContainer container) where TTo : TFrom
        {
            container.RegisterType<TFrom, TTo>(new Singleton());
 
            // important: "touch" it, so that we always have a ready instance
            container.Resolve<TFrom>();
 
            return container;
        }
    }
}

The preparation of the Publisher as well as the Subscriber is now very easy. You only have to mark your code with the Attributes “Publishes” and “SubscribesTo”:

[Publishes("NoteBeforeDelete")]
public event EventHandler NoteBeforeDelete;
 
[SubscribesTo("NoteBeforeDelete")]
public void NoteBeforeDeleteSubscriber(object sender, EventArgs eventArgs) { ... }

Now I’m able to inform other repositories / services that I’m going to delete some data. For my convenience I’m using “EventArgs<T>” and the Extension method “Fire” (both are included in the sources):

this.NoteBeforeDelete.Fire(this, new EventArgs(id));

That’s it. The Event Broker works like a charm. Feel free to test this on your own by deleting some of the provided data:

Download: WebNoteMvc_AOPflavored.zip (1,95 MB) *



I’m planning to write a bit more about „AOP flavored“ stuff from Microsoft. It’s only “flavored”, since it only gives you the feeling of using aspects. In fact no integration into the IL code or proxies are used here.

Tags |