Wednesday, December 23, 2009

StructureMap – Part 2




Before reading this post, you might want to read StructureMap – Part 1. It’s a little more basic than this post and I’m more confident that that one is ALL correct :) What I’ll be showing below, there may be better ways, but I’m pretty new to StructureMap too.

In my first post, I had a mapping from IMailingListSubscriber to FluentMailingListSubscriber. Well, my wife had to inform me that she didn’t just want the subscriber to write to a database, she also wants to be notified and for the subscriber to be notified that it was successful via email. So I thought…well great! How am I going to do that with StructureMap? Luckily for me I had just attended an IoC workshop hosted by Headspring in Austin and they happened to go over just this type of thing. Let me show you how I modified the class before I get into the StructureMap.

I created a new class called MailingListSubscriberWithNotifier and it implements the IMailingListSubscriber interface, which you can see in this post. Well my new class depends on IMailingListSubscriber and INotificationService, so I pass both of them in via the constructor like so:

        private readonly IMailingListSubscriber _mailingListSubscriber;
private readonly INotificationService _notificationservice;
public MailingListSubscriberWithNotifier(IMailingListSubscriber mailingListSubscriber, INotificationService notificationService)
{
_mailingListSubscriber = mailingListSubscriber;
_notificationservice = notificationService;
}


So obviously I’m taking advantage of the Decorator Pattern here and I’m able to decorate the subscriber with a notify like below.

        public void Subscribe(string emailAddress)
{
_mailingListSubscriber.Subscribe(emailAddress);
_notificationservice.Notify();
}


One thing I have not shown is the INotificationService, which looks like this:

public interface INotificationService
{
void Notify();
}


I like keeping it simple :)



Okay, so how do I inject all this into my UI? It’s pretty easy too…easier than I thought it would be!



Here’s the StructureMap code:

            ForRequestedType<IMailingListSubscriber>()
.TheDefault.Is.OfConcreteType<
FluentMailingListSubscriber>()
.EnrichWith(x =>
new MailingListSubscriberWithNotifier(x, DependencyRegistrar.Resolve<INotificationService>()));


What this says is for the requested type of IMailingListSubscriber the default is FluentMailingListSubscriber and enrich it with MailingListSubscriberWithNotification and when creating MailingListSubscriberWithNotification, inject a new instance of INotificationService. This may or may not be important to note, but StructureMap never actually calls “new” on anything it injects. I was taught that at the Headspring workshop, but Matt Hinze did not mention how it actually does create the instances.



I also had to add to my original configuration the mapping for the INotificationService, which looks like this:

  ForRequestedType<INotificationService>()
.TheDefault.Is.OfConcreteType<
EmailNotificationService>()
.CtorDependency<
IMailNotification>();


This code is a little different from the others in that it has a constructor dependency of IMailNotification. I will go into more detail on how to handle the IMailNotification dependency on the next post about StructureMap because I’m going to provide an example on how to setup different instances of an interface.



Please let me know your thoughts! Thanks for reading!

kick it on DotNetKicks.com

blog comments powered by Disqus
Related Posts Plugin for WordPress, Blogger...