利用邮政库从业务层 [英] Using Postal Library From the Business Layer

查看:251
本文介绍了利用邮政库从业务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ASP.NET MVC3其中如下分层建立了一个网站:

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


  • ProjectName.Shared(模型+服务合同)

  • ProjectName.Infrastructure

  • ProjectName.Data
  • ProjectName.Data.Sql

  • ProjectName.Managers(业务层)

  • ProjectName.Services(WCF服务)

  • ProjectName.UI.Main(ASP.NET MVC3应用程序)

这其实是我最近与一些重构后上来的结构。不幸的是,用户界面​​和我的业务层之间没有完全分离,所以这方面可以使用一些更多的重构 - 这是我现在不能因为我在紧张的最后期限做

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.

至于现在,我的客户已要求涉及发送电子邮件给用户后,一些事件发生的一些新功能。该网站目前发送的电子邮件的方式是使用邮政库这是做伟大至今。从MVC Web应用程序这是我对新功能的情况不同内发生...

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...

问题:

我有,我需要让我的业务层中的私有方法发送了电子邮件给使用者的情况。我之所以不能做到这一点的操作方法里面,因为有许多code路径,最终结束调用此私有方法。因此,它不是真正具体到某个操作方法。

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.

建议的解决方案:

我心目中是创建一个 EmailsController ,我可以通过HTTP-POST调用它的方法,从我的业务层中调用。然后,我会通过通过操作方法的参数发送电子邮件所需的参数。

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.

推荐答案

你能不能只是包装你的电子邮件背后的逻辑服务层或基础设施或任何从控制器调用它,并通过构造函数注入的服务?
你做同样的业务层。除邮政对象需要的东西继承到ASP.NET?

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

这篇关于利用邮政库从业务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆