如何在不破坏服务层的情况下使用 MVCMailer?

发布于 2024-10-27 13:24:21 字数 357 浏览 0 评论 0原文

我正在考虑使用 MVcMailer 制作更好的电子邮件。

然而,我不确定的一件事是如何组织代码。我目前有 2 个项目。一份用于 mvc,一份用于我的存储库和服务层。

我的第二个项目不了解 MVC,我想保持这种状态。

我认为我的 smtp 代码将进入服务层或包装器,然后当我需要发送电子邮件时我会从其他服务层调用它。

那么 MVC 邮件程序适合在哪里呢?我是否在控制器中生成正文,然后将其传递到服务层,再将其传递给我的 smtp 类?

I am looking into using MVcMailer to make nicer emails.

However one thing that I am unsure is how to organize the code. I currently have 2 projects. One for mvc and one for my repos and service layers.

my 2nd project has no knowledge of MVC and I would like to keep it that way.

I am thinking that my smtp code would go into a service layer or wrapper and then I would call it up from other service layers when I need to send emails out.

So where does the MVC mailer fit in? Do I generate the body in the controller then pass it to a serivce layer that passes it to my smtp class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2024-11-03 13:24:21

我的解决方案是在服务层中构建接口,然后我的服务可以使用该接口来获取邮件消息,而创建消息的实现继续驻留在 Web 层中。

接口位于服务层:

public interface IMailMessage
{
    void Send();
    void SendAsync();
}

public interface IUserMailer
{
    IMailMessage Welcome(WelcomeMailModel model);
}

实现位于 Web (MVC) 项目中:

public class MailMessage : MvcMailMessage, IMailMessage
{

}

public class UserMailer : MailerBase, IUserMailer
{
    public UserMailer()
    {
        MasterName = "_Layout";
    }

    public IMailMessage Welcome(WelcomeMailModel model)
    {
        var mailMessage = new MailMessage();
        mailMessage.SetRecipients(model.To);
        mailMessage.Subject = "Welcome";

        ViewData = new System.Web.Mvc.ViewDataDictionary(model);
        PopulateBody(mailMessage, "Welcome");

        return mailMessage;
    }
}

最后,在服务层中,邮件程序接口是服务的依赖项:

public class UserCreationService : IUserCreationService
{
    private readonly IUserRepository _repository;
    private readonly IUserMailer _userMailer;

    public UserCreationService(IUserRepository repository, IUserMailer userMailer)
    {
        _repository = repository;
        _userMailer = userMailer;
    }

    public void CreateNewUser(string name, string email, string password)
    {
        // Create the user's account
        _repository.Add(new User { Name = name, Email = email, Password = password });
        _repository.SaveChanges();

        // Now send a welcome email to the user
        _userMailer.Welcome(new WelcomeMailModel { Name = name, To = email }).Send();
    }
}

当它在依赖项注入中连接时,Web.UserMailer 对象是用于 Services.IUserMail 参数来构造 UserCreationService 对象。

我试图使示例保持简单,以便易于理解,但是一旦您在服务层中引用了 IMailMessage,您就可以将其发送到您的 SMTP 代码,而不是像我那样只调用 Send()。出于您的目的,您可能需要进一步充实 IMailMessage 接口,以便访问 MvcMailMessage 类的其他部分。

My solution was to build interfaces in the service layer that my services could then use to get a mail message, while the implementation that creates the message continues to reside in the web layer.

The interfaces are in the service layer:

public interface IMailMessage
{
    void Send();
    void SendAsync();
}

public interface IUserMailer
{
    IMailMessage Welcome(WelcomeMailModel model);
}

The implementations are then in the web (MVC) project:

public class MailMessage : MvcMailMessage, IMailMessage
{

}

public class UserMailer : MailerBase, IUserMailer
{
    public UserMailer()
    {
        MasterName = "_Layout";
    }

    public IMailMessage Welcome(WelcomeMailModel model)
    {
        var mailMessage = new MailMessage();
        mailMessage.SetRecipients(model.To);
        mailMessage.Subject = "Welcome";

        ViewData = new System.Web.Mvc.ViewDataDictionary(model);
        PopulateBody(mailMessage, "Welcome");

        return mailMessage;
    }
}

Finally in the service layer, the mailer interface is a dependency of the service:

public class UserCreationService : IUserCreationService
{
    private readonly IUserRepository _repository;
    private readonly IUserMailer _userMailer;

    public UserCreationService(IUserRepository repository, IUserMailer userMailer)
    {
        _repository = repository;
        _userMailer = userMailer;
    }

    public void CreateNewUser(string name, string email, string password)
    {
        // Create the user's account
        _repository.Add(new User { Name = name, Email = email, Password = password });
        _repository.SaveChanges();

        // Now send a welcome email to the user
        _userMailer.Welcome(new WelcomeMailModel { Name = name, To = email }).Send();
    }
}

When it's wired up in dependency injection, a Web.UserMailer object is used for the Services.IUserMail parameter to construct the UserCreationService object.

I've tried to keep the example simple so that it is easy to follow, but once you have a reference to an IMailMessage in the service layer, you can send it to your SMTP code instead of just calling Send() as I do. For your purposes, you may need to flesh out the IMailMessage interface more in order to access other parts of the MvcMailMessage class.

や三分注定 2024-11-03 13:24:21

MVCMailer 似乎已经支持发送电子邮件。如果您正确设置配置,它应该能够通过电子邮件发送填充的 MailerViews,而无需额外实施。

我不确定您的第二个项目在您的解决方案中的作用,但有两种可能性:

  1. 可能不实用......等等
    对于带有“电子邮件发送
    来自后台进程”

  2. 忘记第二个项目中的 Smtp
    使用 Http 只需调用 View
    这反过来会调用 MVCMailer

MVCMailer seems to already have support for sending email. If you setup the config correctly it should be able to email the populated MailerViews without additional implementation.

I am not sure of the role of your second project in you solution but here are two possibilities:

  1. Probably not practical ... Wait
    for the version with "Email Sending
    from a Background Process"

  2. Forget about Smtp from your second project
    and using Http just call a View
    which in turn would call MVCMailer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文