开发者

Recommended practice to store a mail template

My application sends notification mails to users so I created a html template for each type of notification. I set something like fields in the template as {n} in order to use something like this when I'm sending the message:

string bodyTemplate = GetBodyTemplate(); //gets the html template with {n} in it
message.Body = String.Format(bodyTemplate, fieldZeroValue, fieldOneValue);

For example, the template can have a piece of this:

<td style="vertical-align:middle;padding:0.5em 0.5em 0.5em 0.5em;">
    <a href="www.mypage1az.com\SomePage.aspx?Id={0}">Go to page</a>
</td> 

In this examp开发者_Python百科le I would use:

message.Body = String.Format(bodyTemplate, IdValue.ToString());

Here is my question: Where should I store those very long string with the html templates? I don't want to connect to the the database to get them and storing them in string constants looks awful.

Please tell me what is the recomended practice to store those strings.


I create text files and store the templates in the project. If there aren't too many, you could build them into your assembly and get them out of the Resources. If there are a lot, I would point to a directory that contains the files and use a string reader to pick out a filename.


I heard a pearl of wisdom the other day over an argument of using #regions or not. The "pro #regions" guy said, "But I want to hide ugly code" and the "anti #regions" guy said "if it's so ugly, you shouldn't have written it."

It made me laugh to see that you were getting a code smell from ugly constants.

What is the aversion to putting it in the db? is it the cost of having to get it frequently? If that's so, then perhaps cache it and re-pull from the db at a reasonable interval. That way you can maintain the flexibility of being able to add or change templates without having to hit the db every time.

If constants would genuenly work and you're bawlking that they're ugly, then hide them with a #region :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜