Question about Constants for data lookup
I have the following layers in my project Web/UI, Service, Repository, Common and some other that I don't think matter for this case.
I have a email table that keeps all the site ge开发者_StackOverflow中文版nerated email data in it. I also have a table called EmailType which keeps track of which email in the emails table should be called.
In my email service I have the following method.
public string SendPurchaseConfirmationEmail(string email, string firstName, string dealName) {
Email email = EmailRepository.GetByCurrentByType(EmailType.PurchaseEmail.GetStringValue());
variables = createVariablesList();
SendEmail(email.ListId, email.externalEmailId, variables);
}
EmailType is a unum with an extension to get the value attribute which is a guid and is stored in my repository. The EmailType table works great in the admin which generates test emails but for the actual Web layer the call to the service needs the hard coded values in the repository.
My question is, is this the best or recommended way to do this or can I make this more dynamic in some way? The part I don't like about this is my service layer needs a method for every email since the web layer should touch the repository.
Also would placing the enum in the service layer be incorrect placement if seperation of code was a concern.
Thanks,
Yes, you're going to need to repeat some 'key' value for the set of email types in both your 'service' layer and in your DB.
If you don't use the 'foreign key' values stored in your email table (or other values that easily map to those values in your DB code), then you're going to need to perform a DB lookup with your email type table.
I'd suggest using an integer surrogate key for the email type table and creating a set of named numeric constants that maps to those surrogate key values in any layer that needs to use that set of types (especially if that layer connects to the DB itself). Every other layer should use that set of name constants via one of these layers.
Change the service method to this
public string SendConfirmationEmail(EmailType selectedType, string email, string firstName, string dealName) {
Email email = EmailRepository.GetByCurrentByType(selectedType);
variables = createVariablesList();
SendEmail(email.ListId, email.externalEmailId, variables);
}
With regards to the "web layer" call (calling service from the view), I don't know what exactly you mean but if it is AJAX call then you can just use the enum for the call. Other than AJAX, I don't really know what kind of calls can you make because you basically have GETs and POSTs in MVC. And when you make a POST to the controller action you can again just use the enum.
Like this
EmailService.SendConfirmationEmail(EmailType.Purchase, email, fname, dealname);
Of course, the above sample is valid if you make your service methods static.
精彩评论