Should I send emails at the service level or at the controller level?

I am using the MVC pattern in ASP.NET, using service levels (BLL) and repository for data management. In some cases, I want to send an automatic email when a new request is sent through our website. In which layer of architecture should this email be sent? In the controller layer or service level? I was thinking about the level of service, since this is where the "business logic" should go, but I'm 100% not sure if this is semantically correct.

Edit: When I say β€œnew query”, I mean that the user takes an action that is stored in some kind of data store. As an example, they create a new β€œProject” on a website. Thus, the request will go through the levels of the controller> services> repository.

+6
asp.net-mvc
source share
7 answers

I agree. They belong to the service layer. The controller should only be very thin; just by calling the appropriate business / data / any classes and deleting the data in the same way.

+16
source share

I always create my business layer so that it works regardless of the environment through which the data is presented. So, for example, if I sent an email when creating a new account, I would like it to happen regardless of whether the user creates an account through a website or desktop application. In this case, e-mail will be sent in the business layer, because it is common for both carriers.

+5
source share

In most cases, you can clarify these issues by thinking about what you would like to do if you had a web service level or a Windows application in front of your BLL instead of your web application. Do you still want the same email address to come out? If so, sending emails is part of your business model and it should go to BLL. if the answer is no, then sending emails is really the logic of the application, and it should go in your application layer.

+5
source share

I would tie it to the service layer.

+4
source share

in the mix and pdc phill said in his speech

thin controllers, fat models 
+1
source share

In fact, I have control actions in my controllers. Any other business logic still takes on some other level.

0
source share

If this is a core business function, I would put it at the service level.

However, I could abstract the "MailSender" class so that my service level is not explicitly tied to sending emails in a certain way (for example, using System.Web.Mail). You might want to use another method later (for example, sending mail asynchronously using queues). It also allows you to safely unit test without spamming anyone (replacing MailSender with those who do not actually send mail) :)

0
source share

All Articles