views:

356

answers:

7

I am using the MVC pattern in ASP.NET using service (BLL) and repository layers for data managment. In some cases, I want to send out an automatic e-mail when a new request is sent through our website. In what layer of the architecture should this e-mail be sent? In the controller layer or the service layer? I was thinking the service layer, since that is where "business logic" is supposed to go, but am not 100% sure if that is semantically correct.

Edit: When I say "new request" I mean that a user takes an action that saves to some sort of datastore. As an example they create a new "Project" on the website. So the request will pass through the controller > service > repository layers.

+2  A: 

I would stick it in the service layer.

Daniel A. White
+8  A: 

Agreed. They belong in the service layer. The controller should only be very thin; just calling the appropriate business/data/whatever classes and dropping the data in the coinciding view.

swilliams
+1  A: 

I always design my business layer so that it's capable of working regardless of the medium through which the data is presented. So, for example, if I were to send an email when a new account is created, I would like this to occur regardless of whether the user is creating the account via a website or a desktop application. In that case, the sending of the email would occur in the business layer because it is common to both mediums.

David Brown
A: 

at mix and pdc phill said in his talk,

thin controllers, fat models
Chris
A: 

Actually I just have Controller Actions in my Controllers. All other business logic is taken care of by some other layer whatever that may be.

mhenrixon
+1  A: 

Most of the time you can clarify issues like this by thinking about what you would want to happen if you had a web service layer or a windows app in front of your BLL instead of your web app. Would you still want the same email to go out? If the answer is yes, then sending the email is part of your business model and it should go in the BLL. if the answer is no, then sending the email is really application logic and it should go in your app layer.

Rudy Lacovara
This is a very smart way to think about it...thanks for the tip!
A: 

If it's a core business function I would put it in the service layer.

I might however abstract a "MailSender" class so that my service layer isn't explicitly tied to sending email in a particular way (e.g. using System.Web.Mail). You may want to use another method later on (e.g. sending mail asyncronously using queues). It also allows you to unit test safely without spamming anyone (by replacing MailSender with one that doesn't actually send any mail) :)

JonoW