Steve Gentile recently directed me to Udi Dahan’s recent post his improvement on the Domain Event pattern, and after reading the post, digesting it and thinking about his approach I have decided to include it into NCommon.

The Domain Event pattern aims to provide a solution for being able to create fully encapsulated domain models. The term fully encapsulated domain models refer to the approach where the entities in your domain model are not bound or depend on external services for any purpose or operation.

A good example for this, although a beaten to death example, is the email sending or rather the notification example. Lets say when an order’s status has changed an email notification has to be sent out to the customer of the order informing them of this status change. My previous approach has been to use the Service Locator pattern to get a service instance of IOrderStatsNotifier and use the IOrderStatusNotifier instance to send out the emails.

Semantically that has always bothered me because the domain entity really should not be informing the notifier that the order’s status has changed, rather it should be the other way around. The Notifier should automatically be able to handle order’s status change and do the necessary. With the Domain Events pattern this becomes ridiculously easy.

If you haven’t read Udi’s post, I’d strongly recommend reading it. Anyways, the implementation of Domain Events pattern in NCommon is almost identical to that shown in Udi’s post. Below is a test from NCommon that shows how the Domain Event pattern can be used:

[Test]

public void registered_handlers_are_executed_when_event_is_raised ()

{

    var mockTest1Handler = MockRepository.GenerateMock<Handles<TestEvent1>>();

    var mockTest2Handler = MockRepository.GenerateMock<Handles<TestEvent2>>();

    var locator = MockRepository.GenerateStub<IServiceLocator>();

 

    locator.Expect(x => x.GetAllInstances<Handles<TestEvent1>>())

           .Return(new[]{mockTest1Handler});

 

    ServiceLocator.SetLocatorProvider(() => locator);

 

    DomainEvent.Raise(new TestEvent1());

    mockTest1Handler.AssertWasCalled(x => x.Handle(null), options => options.IgnoreArguments());

    mockTest2Handler.AssertWasNotCalled(x => x.Handle(null), options => options.IgnoreArguments());

}

The implementation of Domain Events pattern is available in trunk.

Posted on Wednesday, June 17, 2009 10:48 PM | Filed Under [ Patterns ]


Comments

Gravatar
# Please do some more blogging :).
Posted by Anonymous
on 10/21/2009 1:29 PM
Please do some more blogging :).
Gravatar
# You alive out there?
Posted by Anonymous
on 9/30/2009 9:50 PM
You alive out there?
Gravatar
# Thanks for the code update and explanation!
Posted by Steve Gentile
on 7/2/2009 11:37 AM
Thanks for the code update and explanation!
Gravatar
# Oh wicked thanks to you and Udi for ticking off an...
Posted by Jake Scott
on 6/18/2009 11:00 AM
Oh wicked thanks to you and Udi for ticking off another one of those little problems that I have encounted with DDD :)
Gravatar
# re: Domain Events pattern in NCommon
on 6/25/2012 3:01 AM
This is a good topic in my photography subject and I'll definitely get an A grade if I use this post as one of my examples but of course, credits will be yours.
Gravatar
# re: Domain Events pattern in NCommon
Posted by Cheap Ugg boots
on 12/7/2012 1:06 AM
in of a use of colors and sizes, are great for putting together your computer or laptop is actually in.
Gravatar
# re: Domain Events pattern in NCommon
Posted by rolex replica
on 1/5/2013 12:37 AM
Then replica wrist rolex replica are extremely compliant wrist watches owing to their distinctive colours and glasses. consequently if you ever require any help concerning the replica rolex, please really feel free of charge to get in touch with us online. We should certainly supply you the most effective rolex replica watches.
Gravatar
# re: Domain Events pattern in NCommon
Posted by roysalbert
on 1/23/2013 2:30 AM
The Domain Events pattern in NCommon i have so many information's about this article.dissertation assistance | buy dissertation uk | dissertation in a week | professional dissertation writers uk | custom dissertation proposal writing.




Gravatar
# re: Domain Events pattern in NCommon
Posted by billy
on 1/23/2013 7:47 AM
Steve Gentile you are a marvelous person because shares so many get articles.copywriting agency
Gravatar
# ray ban sunglasses cheap
on 2/20/2013 8:15 PM
Having a small cheap oakley sunglasses forehead plus face plus http://www.cheapsunglassesmall.org extended cheek bone fragments cheap ray ban sunglasses, rimless solar shades http://www.cheapray-bansunglassess.co.uk look ideal with a gem cheap nike free run australia contour skin. That will become softer your angular http://www.nikefreerunsalesau.org/ contour, solar shades which may have ray ban sunglasses cheap a great oval or even slightly bent http://www.discount-rayban.us/ sq contour oakley sunglasses outlet job ideal http://www.oakleysoutletcheap.org.
Gravatar
# re: Domain Events pattern in NCommon
Posted by Wizardw
on 2/21/2013 12:47 AM
Read customers hermes handbags feedback of the product you want to buy. Beware of cheap replicas. Low grade replicas are composed of foam or replica hermes plastic or vinyl. They have poor quality stitches and do not have the exact size and structure of the original one. You can buy a wide variety of these hermes bags
Gravatar
# re: Domain Events pattern in NCommon
on 4/8/2013 9:39 PM
Marc Jacobs Online outlet is really in a position to be calling someone else ridiculous, we guess he, himself, does tods shoes dress pretty simply.When I've done Marc Jacobs Handbags, this and this, then I can do that. When I've gotten through this really difficult time at work, then I can start getting fit Marc By Marc Jacobs.Golden, not orange. Gradual, not garish. If only Marc jacobs had thought of this before she attempted to create her own line of http://www.marcjacobsonline.co.uk/ Marc Jacobs Sale-tanners, if you can even call them that which they with seven other http://www.acoachfactoryoutletusa.com coach factory outlet. For more about the Shop Marc Jacobs at NET-A-PORTER in general and this year's tods handbags in particular.But it takes some thought - namely to belt or not to belt, that is the question. Remember, a belted mac - and for this read "cinched-in Tods Men Ferrari Shoes" - can look a little feminine,or you may need one less depending on how large your coach factory outlet are..You could also try pairing it with their Marc Jacobs Online purse (鈧�30), which is a white, tiered gypsy inspired coach factory outlet and marc jacobs style.
Gravatar
# re: Domain Events pattern in NCommon
Posted by 11
on 5/6/2013 8:23 AM
Applied three new shades of "browns cheap hair weave" from the Great kinky curly hair weave Lengths colour shades together to give Rochelle a slightly lighter look than her previous virgin hair weave. Rochelle, who is a regular client best weave hair at Inanch London, also opted for cheap weave longer length and had her human hair weaves hair styled in to loose natural hair weave. Looking good Humes!
Post Comment
Title *
Name *
Email
Url
Comment *  
Please add 5 and 5 and type the answer here: