寻找一个很好的技术来存储电子邮件模板 [英] Looking for a good technique for storing email templates

查看:93
本文介绍了寻找一个很好的技术来存储电子邮件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了我们正在适度使用的电子邮件模板的网站。正如我们在其中传递令牌,如{用户名},HTML模板{电子邮件},{} NameFirst等。

I am building a site in which we are making moderate use of email templates. As in, HTML templates which we pass tokens into like {UserName}, {Email}, {NameFirst}, etc.

我跟到哪里存储这些努力,尽可能最佳实践去。我会首先显示我采取的方法,而我会很兴奋听到一些专家的观点为据其他方法。

I am struggling with where to store these, as far as best practice goes. I'll first show the approach I took, and I'd be really excited to hear some expert perspective as a far as alternate approaches.

我一个名为/模板/.

我所说的静态方法在我的服务层,这需要在以下参数:

I call a static method in my service layer, which takes in the following arguments:


  1. 用户名

  2. 用户名

  3. 电子邮件

  4. TEMPLATEPATH(〜/模板)

  5. 电子邮件主题

在服务层我有我的静态方法SendUserEmail(),它利用模板类的 - 这需要一个路径,加载它作为一个字符串,并具有AddToken()方法

Within the service layer I have my static method SendUserEmail() which makes use of a Template class - which takes a path, loads it as a string, and has a AddToken() Method.

在我的静态SendUserEmail(),我建立令牌列表关闭方法签名,并发送电子邮件。

Within my static SendUserEmail(), I build the token list off of the method signature, and send the email.

这使得我在实际使用相当长的方法调用,特别是因为我在web.config中的TEMPLATEPATH和电子邮件主题打来的。我可以创建具有比ConfigurationManager.AppSettings较短的方法调用一个工具,但我关心的是更多的,我通常不会看到方法签名这个长,我觉得这是因为我做错了什么。

This makes for a quite long method call in my actual usage, especially since I am calling from the web.config the "TemplatePath", and "Email Subject". I could create a utility that has a shorter method call than the ConfigurationManager.AppSettings, but my concern is more that I don't usually see method signatures this long and I feel like it's because I'm doing something wrong.

此技术的伟大工程的电子邮件,我现在有,这在大多数使用的是前3令牌。然而,在未来,我将有更多的令牌传递,而我只是想知道采取什么办法。

This technique works great for the emails I have now, which at the most are using the first 3 tokens. However in the future I will have more tokens to pass in, and I'm just wondering what approach to take.

我创建特定的电子邮件需要方法发送? IE浏览器。 SendNewUserRegistration(),SendMarketingMaterial(),并且每个具有用于参数的不同签名σ

Do I create methods specific to the email needing to be sent? ie. SendNewUserRegistration(), SendMarketingMaterial(), and each has a different signature for the parameters?

我使用ASP.NET成员,其中包含可能是延长我永远需要的所有领域。主要有三个目的,aspnet_User,aspnet_Mebership和aspnet_profile。如果这一切都是在一个对象所包含的,我也刚刚通过,在,有没有与传递的所有3,获得我需要的所有领域的性能问题?这是对刚刚传入aspnet_User.UserID,aspnet_User.Email等?

I am using ASP.NET Membership, which contains probably the extend of all the fields I'll ever need. There are three main objects, aspnet_User, aspnet_Mebership and aspnet_profile. If it was all contained in one object, I would have just passed that in. Is there performance concerns with passing in all 3, to get all the fields I need? That is versus just passing in aspnet_User.UserID, aspnet_User.Email, etc?

我可以看到传递与令牌项目的字典,但我只是想知道,如果这是过分的要求调用页面?

I could see passing in a dictionary with the token entries, but I'm just wondering if that is too much to ask the calling page?

有没有办法在它的配置文件,坚持这些自己称为Templates.config,其中有像标签 -

Is there a way to stick these in a config file of it's own called Templates.config, which has tags like -

<Templates>
<EmailTemplate Name="New User Registration">
<Tokens>
<UserName>
<UserID>
<Email>
</Tokens>
<Message Subject="Hi welcome...">
   Hi {UserName}...
</Message>
</EmailTemplate>
</Templates>

我想我要问,主要的原因是因为我有一个很难确定在何处的责任应该是尽可能确定使用什么模板,以及如何传递参数。它是确定的,如果调用页面已经建立TokenName,TokenValue的词典吗?还是应该采取的方法在每一个定义的参数?这看起来出在web.config的地方,因为我有2个条目和,而且感觉像它应该看起来更嵌套。

I guess the main reason I'm asking, is because I'm having a hard time determining where the responsibility should be as far as determining what template to use, and how to pass in parameters. Is it OK if the calling page has to build the dictionary of TokenName, TokenValue? Or should the method take each in as a defined parameter? This looks out of place in the web.config, because I have 2 entries for and , and it feels like it should look more nested.

感谢您。任何技术或客观的态度的建议,我可以用它来问我的方法是否确定。

Thank you. Any techniques or suggestions of an objective approach I can use to ask whether my approach is OK.

推荐答案

所有我想建议你首先要使用 NVelocity 的作为模板引擎。至于主要的问题我觉得你可以创建一个抽象类的 MAILMESSAGE 的,并得出每一个需要的每个消息(具有独特的模板)。所以,你会使用这个类似以下内容:

First of all I would like to suggest you to use NVelocity as a template engine. As for main problem I think you can create an abstract class MailMessage and derive each one for every needed message (with unique template). So you will use this like following:

MailMessage message = new UserRegistrationMessage(tokens);
//some code that sends this message

去这样可以强制每个具体XXXMessage类是负责存储模板,填充它与给定的标记的。如何处理标记?该simpliest办法是将它传递给消息之前创建一个字典,所以每个具体消息类就知道如何应对通过字典和从哪些记号它应该包含,但你还需要记住什么记号它应该包含的内容。另一种方法(我喜欢它更多)是创建一个一般的抽象类型的 TokenSet 的和派生一个需要每一个独特的标记。它TokenSet 的几个特性:例如,您可以创建一个 UserMessageTokenSet

Going this way you force each concrete XXXMessage class to be responsible for storing a template and filling it with the given tokens. How to deal with tokens? The simpliest way is to create a dictionary before passing it to the message, so each concrete message class will know how to deal with passed dictionary and what tokens it should contain, but you also need to remember what tokens it should contain. Another way (I like it more) is to create a general abstract type TokenSet and a derived one for every needed unique set of tokens. For example you can create a UserMessageTokenSet : TokenSet and several properties in it:

UserNameToken
SomeUserProfileDataToken

等。因此,使用这种方式,你将永远知道,什么样的数据,你应该为每个设置标记设置和结果
UserRegistrationMessage会知道怎样从这个tokenSet服用。

etc. So using this way you will always know, what data you should set for each token set and
UserRegistrationMessage will know what to take from this tokenSet.

有很多方法去。如果你将描述任务更好,我想我会尽量建议你更具体的东西。但总的想法是上面列出。希望它可以帮助=)

There are a lot of ways to go. If you will describe you task better I think I will try suggest you something more concrete. But general idea is listed above. Hope it helps =)

这篇关于寻找一个很好的技术来存储电子邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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