Oguz Bilgic
Oguz Bilgic

Reputation: 3480

General vs Model-specific observers and mailers?

When creating mailers and/or observers do you organize them based on models or based on their task?

based on models

PictureMailer
PictureObserver
EventMailer
EventObserver
UserMailer
UserObserver

or based on tasks

AdminMailer (Mails to be sent to admins)
AdminObserver 
NotificationMailer
NotificationObserver

Upvotes: 2

Views: 160

Answers (1)

Stefan Kanev
Stefan Kanev

Reputation: 3040

I organize them based on their tasks. I prefer having more, small, fine-grained mailers over a few, larger, coarser-grained ones. For example, I'd probably not have AdminMailer, but a couple of different mailers for different things that happen in the admin. Same goes for notifications - you probably have more than one use case that sends notifications, so I'd have a mailer for each. Generally, one can think about it as mailer-per-feature.

As for the observers, it depends on their task. If I'm monitoring creating and modifying of users for analytics, I'll probably have just a single UserObserver. I'll rename it to UserAnalyticsObserver as soon as I need a second observer on user. Looking at a current project, I have four observers and only one of them is named after the model - most are named after the feature.

My reasoning here is following the Single Responsibility Principle, which essentially means that one class should have one reason to change. If I go with naming mailers/observers after the models, I end up with classes that support multiple features, thus, multiple reasons for change.

Upvotes: 1

Related Questions