Using Postal Library From the Business Layer

672 views Asked by At

I have a website built using ASP.NET MVC3 which is layered as follows:

  • ProjectName.Shared (model + service contracts)
  • ProjectName.Infrastructure
  • ProjectName.Data
  • ProjectName.Data.Sql
  • ProjectName.Managers (Business layer)
  • ProjectName.Services (WCF services)
  • ProjectName.UI.Main (ASP.NET MVC3 application)

This is actually the structure I recently came up with after some refactoring. Unfortunately, there isn't complete separation between the UI and my business layer so this area could use some more refactoring - which I cannot do right now because I'm on a tight deadline.

As for now, my client has requested some new features which involve sending emails to users after some events happen. The way the website currently sends emails is by using the Postal library which is doing great so far. That happens from within the MVC web application which is different from the case I have for the new features...

The Problem:

I have the case where I need to let a private method inside my business layer to send out the emails to users. The reason why I cannot do that inside an Action method is because there are many code-paths that eventually end up calling this private method. So it's not really specific to one Action method.

Proposed Solution:

What I have in mind is to create an EmailsController which I can call its method through HTTP-POST calls from within my business layer. I would then pass on the parameters needed to send the email through the arguments of the Action method.

But I do find this solution to be kinda flawed and hacky. So I would really like to hear some thoughts on how to approach this problem.

1

There are 1 answers

3
Jafin On

Could you not just wrap your email logic behind a service layer or infrastructure or whatever and call it from the controllers, and inject the service via your constructor? You do the same with your business layer. Unless the postal object requires something inherit to asp.net ?

namespace ProjectName.Infrastructure
{
    public interface IEmailService
    {
        void SendEmail(string address, string content)
        {
        }
    }

    public class EmailService : IEmailService
    {
        private void SendEmail(string address, string content)
        {
            //...send email via Postal
        }
    }
}

public class ControllerA
{
    private IEmailService emailService;

    public ControllerA()
    {
        emailService = new EmailService();
    }

    public ActionResult SendEmail(ViewModel model)
    {
        emailService.SendEmail(model.address, model.content);
    }
}