Using Postal Library From the Business Layer

喜欢而已 提交于 2019-12-07 07:47:38

问题


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:


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);
    }
}


来源:https://stackoverflow.com/questions/9148745/using-postal-library-from-the-business-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!