关于数据查找常量的问题
我的项目中有以下几层:Web/UI、服务、存储库、公共层以及其他一些我认为对本例不重要的层。
我有一个电子邮件表,其中保存了所有网站生成的电子邮件数据。我还有一个名为 EmailType 的表,它跟踪应该调用电子邮件表中的哪些电子邮件。
在我的电子邮件服务中,我有以下方法。
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 是一个带有扩展名的 unum,用于获取 value 属性,该属性是一个 guid,存储在我的存储库中。 EmailType 表在生成测试电子邮件的管理中运行良好,但对于实际的 Web 层,对服务的调用需要存储库中的硬编码值。
我的问题是,这是最好的或推荐的方法吗?或者我可以以某种方式使其更加动态吗?我不喜欢的部分是我的服务层需要为每封电子邮件提供一个方法,因为 Web 层应该接触存储库。
如果需要考虑代码分离,那么将枚举放置在服务层中也是不正确的放置方式。
谢谢,
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 generated 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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您将需要在“服务”层和数据库中为电子邮件类型集重复一些“键”值。
如果您不使用电子邮件表中存储的“外键”值(或可以轻松映射到数据库代码中的这些值的其他值),那么您将需要使用电子邮件类型表执行数据库查找。
我建议对电子邮件类型表使用整数代理键,并创建一组命名数字常量,这些常量映射到需要使用该类型集的任何层中的代理键值(特别是如果该层连接到数据库本身) )。其他所有层都应通过其中一个层使用该组名称常量。
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.
将服务方法更改为此
关于“web层”调用(从视图调用服务),我不知道你到底是什么意思,但如果它是AJAX调用那么你可以只使用枚举进行调用。
除了 AJAX 之外,我真的不知道您还可以进行哪些类型的调用,因为 MVC 中基本上都有 GET 和 POST。
当您向控制器操作发出 POST 时,您可以再次使用枚举。
当然
,如果您将服务方法设为静态,则上述示例是有效的。
Change the service method to this
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
Of course, the above sample is valid if you make your service methods static.