I'd like to know what you think of the following design options.
I have an 'AgendaController' (.NET MVC project) which handles user actions like viewing their agenda, making appointments, etcetera. When a user makes an appointment, the appointment is saved in a database. Then, there is a 'secondary' task that has to be done, in this case sending a message to the user (whether this is a text sms, email, or something else is not relevant). I can implement this in three different ways:
I could simply add a call directly in the controller action, something like
messageService.SendNewAppointmentMessage(data)
. Which is fine, I guess, though it does clutter up the controller action a bit. Specifically, if, because of some future requirement some other action needs to be done (for example, logging although not a good example) I'd get yet another call to some logger class.I could declare a static event in the AgendaController,
OnAppointmentMade
, and create a MessageService that subscribes to this event. Then, when an appointment is saved, the event is fired and the actual message sending handled somewhere else. I could have more classes subscribe to this event if those requirements come up. This way, the controller action stays very clean, and all 'secondary tasks' are performed somewhere else.I could use a custom attribute on the controller action,
[SendNewAppointment]
, and send the message in theOnActionExecuting
method of that attribute. This follows the MVC design nicely I guess, but I don't necessarily like the Attributes clutter: I'd have anAuthorize
,AcceptVerbs
,ActionName
,SendNewAppointment
, etc etc attribute on a single controller action.
Which design is preferred? Which one is certainly a no-go? I have a slight preference for the second design, although I wonder if one should (mis)use events for this. I'd really like to hear your opinions!