具有业务逻辑的视图与代码
对于应用程序,我需要每小时向最终用户发送个性化邀请电子邮件。这些用户的电子邮件地址及其其余信息都在数据库中。
- 所有有一点的用户
Planned.IsPlannable
设置为True
并且 有一个Planned.DateStart
和一个Planned.DateEnd
之间的某个时间 期间可用于接收 电子邮件消息。 - 每小时大约有350 条消息要发送。
- 所有信息表明 必须包含在消息中的是 从数据库中。
- 该应用程序是 .NET4.0 控制台应用程序,用于数据 访问 我使用Subsonic3.0。
至少有两种方案可供选择:通过 1
:) 查看类似的内容检索适当的数据:
SELECT [Computer].ComputerName,
[User].UserEmail,
[Planned].DateAvailable,
[Planned].DatePlanned
FROM [Computer]
INNER JOIN
[Planned] ON [Computer.ComputerID] = [Planned.ComputerID]
INNER JOIN
[User] ON [Computer].UserID = [User].UserID
WHERE (DATEDIFF(dd, GETDATE(), [Planned.DateAvailable]) < 10)
AND Planned.IsPlannable = 1
以及撰写,根据此视图的结果,适当的数据此应用程序C# 代码中的消息。
2:) 在视图中在 sql-server 上撰写整个消息并返回类似的内容
[EmailTo]
[Subject]
[Body]
然后仅迭代结果并从中创建 MailMessage
对象
在这两种情况下我都会收到如下消息:
foreach (vwGetInvitableComputer u in UserController.GetAllInvitableUsers())
{
// where the usercontroller wraps the Subsonic logic.
// the composition happens here
}
Iterate trough that in the C# code and just compose the mailmessage from that preformatted 数据。
选择什么场景对性能和资源来说是明智的?
更新:选项 2 中的数据库中确实存在文本操作。但是,这会将消息正文中的三个字符串替换为 CompterName、DateStart 和 DateEnd。也许 SQL 视图足够智能,可以传播性能,而 C# 代码是否“在请求时”?
For an application I need to send personalized invitation emails to end-users every hour. Those users email-addresses, and the rest of their information are in the database.
- All users that have a bit
Planned.IsPlannable
set toTrue
and
have aPlanned.DateStart
and aPlanned.DateEnd
between a certain
period are available for receiving an
email message. - There are about 350 messages to sent every hour.
- All information that
must be included in the message is
from the Database. - The application is
a .NET4.0 Console application, for data
access I use Subsonic3.0.
There are at least two scenarios to choose from: retrieving the appropriate data via a
1:) View something like:
SELECT [Computer].ComputerName,
[User].UserEmail,
[Planned].DateAvailable,
[Planned].DatePlanned
FROM [Computer]
INNER JOIN
[Planned] ON [Computer.ComputerID] = [Planned.ComputerID]
INNER JOIN
[User] ON [Computer].UserID = [User].UserID
WHERE (DATEDIFF(dd, GETDATE(), [Planned.DateAvailable]) < 10)
AND Planned.IsPlannable = 1
and compose, based on the results of this view, the appropriate messages in the C# code of this application.
2:) Compose the entire message on the sql-server in the view and return something like
[EmailTo]
[Subject]
[Body]
Then only iterate trough the results and create MailMessage
Objects from it
in both cases I get the messages like:
foreach (vwGetInvitableComputer u in UserController.GetAllInvitableUsers())
{
// where the usercontroller wraps the Subsonic logic.
// the composition happens here
}
Iterate trough that in the C# code and just compose the mailmessage from that preformatted data.
What scenario is performance and resources wise to choose?
update: There is indeed text manipulation in the database in option 2. However, this is replacing three strings in the messagbody to the CompterName, DateStart and DateEnd.Perhaps, SQL-views are smart enough to spread the performance for this, while the c# code does it "when requested"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个考虑因素:您能否想象有一天需要更改电子邮件格式?如果没有,那么可能并不重要。
第二个考虑因素:您能否想象有一天,电子邮件需要的操作量将超过 SQL 能够轻松提供的操作量?如果是这样,那么你绝对应该选择 C#。
第三个考虑因素:重新部署会给环境带来多大的问题?
最后还有一个替代选项:使用 C# 进行消息撰写,但使用可以轻松更新的基于数据库或文件的模板。
First consideration: Can you envision a time when changing the emails format will become necessary? If not then it probably doesn't matter.
Second consideration: Can you envision a time when the emails will require more manipulation than SQL is easily capable of delivering? If so then you definitely should choose C#.
Third consideration: How problematic would a redeploy be for the environment?
And finally an alternative option: Use C# for the message composition, but use a database or file based Template that can easily be updated.
我会选择选项 2,使用视图或存储过程在服务器上编写发送电子邮件所需的所有信息。理想情况下,数据库调用应尽可能减少与服务器的往返次数。
I would choose option 2, composing all the information required to deliver the emails on the server either using a view or a stored procedure. Ideally database calls should result in the least number of round trips to the server as possible.