user2309367
user2309367

Reputation: 327

Proper use of command pattern?

I have the following scenario.

I want to be able to send notifications when something bad happens in my application. There are 2 types of notifications that should be available. 1.) Email Notification 2.) Event log Notification

Unfortunately, both of these types of notifications require some "Uncommon" information in order to be useful ( Emails need email address, SMTP Addresses...etc while Eventlogs would never need that information. )

I was thinking of using the CommandPattern to encapsulate and hide these details from my Application Service that sends notifications. Therefore, THe notification service only has a list of commands. When a notification needs to be sent, it simply iterates through it's list of commands and executes them. Is this a viable solution in order to decouple my application service from specific implementations of Notification implementations?

Example of Notification Service

public class NotificationService
{
     IList<ICommand> _notificationCommands;
     void SendNotifications()
     {
         foreach ( vat notification in _notificationCommands)
                 notification.Execute();
     }
}

Is this the proper solution/use of the command pattern?

Upvotes: 0

Views: 142

Answers (1)

David Osborne
David Osborne

Reputation: 6791

The Command pattern might work. However, I would consider an EventAggregator approach where you emit typed events. A much more appropriate solution, IMHO.

http://codebetter.com/jeremymiller/2007/06/29/build-your-own-cab-11-event-aggregator/ http://martinfowler.com/eaaDev/EventAggregator.html

Upvotes: 1

Related Questions