Views with business logic vs code

无人久伴 提交于 2019-12-24 11:27:20

问题


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 to True and have a Planned.DateStart and a Planned.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"?


回答1:


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.



来源:https://stackoverflow.com/questions/6357830/views-with-business-logic-vs-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!