Search code examples
c#-4.0domain-driven-design

DDD: MailService.SendNotificationToUser() or User.Notify()?


I seem to stumble on problem after problem giving my entities behaviour.

I have a system where a user gets a notification when someone comments his article. Right now it is via an e-mail.

I'm struggling how to implement this the DDD way.

Option 1

User entity has a Notify method: User.Notify() The method uses C# built in classes to send an e-mail notification via e-mail

The problem with having this in the domain is that it is technology specific, and how a user is notified might change in the future. I feel this belongs to infrastructure, but how then can a user have behavior?

Option 2

I create a Service: NotificationService.Notify(User) The Service uses C# built in classes to send an e-mail

The pro is that the service could be an Application Service, and as far as I know an application service can use the infrastructure and call things like the System.Net.Mail and repositories for that sake.

How would you implement this?


Solution

  • This operation doesn't seems to be natural part of User entity. This operation is stateless. So, it seems to me that notification is responsibility of service.

    This is not domain service, because its not about business logic. Notification service abstracts infrastructure implementations from your domain (User). Notification could use Emails, MSMQ or put messages to database. This should not affect your domain model. So, thats definitely goes to infrastructure.

    So, I'd go for option 2. Infrastructure service.

    PS Consider about more descriptive method name NotifyCommentAdded