Question

I have a web application with the following layers:

  1. View
  2. Business
  3. Entities
  4. Repository

I want to send an email to a User when he or she is registered.

I have the class User, in the Entities layer, and the classes UserBussines and MailBusiness in the Business layer.

The problem is that I don't know where is correct to send the email to the user.

I see some options

1) In the controller:

UserBusiness.AddUser();
MailBusiness.SendEmail();

2) In the business

UserBusiness.AddUserAndSendEmail()
{
    AddUser();
    MailBusiness.SendEmail();
}

In the two options I think that I'm not fulfilling the SRP in the SOLID principle, because I'm giving two responsibilities in one method.

I'm not understanding the principle? Or am I doing something wrong?

Was it helpful?

Solution

You have user registration scenario:

Given not registered user
When user registers
Then user becomes registered
And greeting mail is sent

This scenario should exist in your business logic layer, because:

  • It is scenario of your business domain
  • It should exist whether you use web application, desktop application, or mobile application.

So, you should have some application service (e.g. UserService) which will act as coordinator for user repository and email service:

public void RegisterUser(User user)
{
    // possibly you should also check that user is not registered yet
    _userRepository.Add(user);
    _emailService.SendGreeting(user);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top