简体   繁体   English

表示层逻辑和服务/实体

[英]Presentation Layer Logic and Services / Entities

I have a .net webform 'NewShift.ascx' file. 我有一个.net网络表单“ NewShift.ascx”文件。 It provides you an interface to add information about a new shift you'll be working (or just worked). 它为您提供了一个界面,用于添加有关您将要工作(或刚刚工作)的新班次的信息。 So things like how many hours you worked, your shift manager, etc. In my code behind I'd like to change the date created label's text to be the current date (DateTime.Now) if and only if it's before 5pm today, otherwise make the date read tomorrow (for reasons I won't go into). 这样的事情,例如您工作了多少小时,您的班次经理等。在我后面的代码中,如果且仅当今天下午5点之前,我想将创建日期标签的文本更改为当前日期(DateTime.Now)。将日期设为明天(出于我不愿讨论的原因)。

In addition, I might want to format the date a certain way based upon the user (for example if they're associated w/ one culture vs. another as per settings saved in the database, not the current country they are browsing the site in). 此外,我可能想根据用户以某种方式设置日期格式(例如,如果数据库中保存的设置将他们与一种文化相对于另一种文化相关联,而不是他们正在浏览该网站的当前国家/地区, )。

You could argue because it's such simple logic, I could just write it out in the code behind, but let's say I had a lot of these little simple things going on (changing values, formatting dates, displaying values based on logic, etc). 您可能会争辩,因为它是如此简单的逻辑,我可以在后面的代码中将其写出来,但是让我们说,我有很多这些简单的事情(更改值,格式化日期,基于逻辑显示值等)。

My understanding is this would be the smart UI anti-pattern if I did all of this in the code behind. 我的理解是,如果我在后面的代码中完成所有这些操作,这将是智能UI的反模式。 Where should the above logic be? 以上逻辑应该在哪里?

I hear about not having an anemic domain model and making sure my entities can provide functionality for answering domain-specific questions (?). 我听说没有贫血的领域模型,并确保我的实体可以提供用于回答领域特定问题(?)的功能。 I also hear about leveraging service layers as much as possible for things as well. 我也听说过尽可能利用服务层。

So if I had to do the following: 因此,如果我必须执行以下操作:

  1. Change a label's text to either today, or tomorrow, based on X and Y 根据X和Y将标签的文本更改为今天或明天
  2. Add two values together and assign a label text to that sum based on a user preference 将两个值加在一起,然后根据用户偏好将标签文本分配给该总和
  3. conditionally hide a checkbox based upon the day of the week 根据星期几有条件地隐藏一个复选框

Where should logic like this be? 这样的逻辑应该在哪里? On the Shift object? 在Shift对象上? Or some sort of _shiftService? 还是某种_shiftService?

Thanks! 谢谢!

  1. Change a label's text to either today, or tomorrow, based on X and Y 根据X和Y将标签的文本更改为今天或明天
  2. conditionally hide a checkbox based upon the day of the week 根据星期几有条件地隐藏一个复选框

Both of these are obviously presentation concerns. 显然,这两个都是表示方面的问题。 This means that the UI control (ascx file) should make these decisions. 这意味着UI控件(ascx文件)应该做出这些决定。 You could add a converter if it makes sense, see IValueConverter Interface for more details. 如果有必要,可以添加一个转换器,有关更多详细信息,请参见IValueConverter接口 It is built for Windows Forms, but you can adapt something similar for the web. 它是为Windows窗体构建的,但是您可以为Web改编类似的东西。 Especially using web forms, I find that this clears up the markup substantially. 尤其是使用Web表单,我发现这可以大大清除标记。


  1. Add two values together and assign a label text to that sum based on a user preference 将两个值加在一起,然后根据用户偏好将标签文本分配给该总和

It would really depend on what these two values are. 这实际上取决于这两个值是什么。 If these values are on your model and you just need to show a sum of them and that sum only makes sense to the presentation and not to the business process, this would be a presentation concern and you would either use a converter in your markup or, if you're following MVVM, you would put that summary in your view model under a property of a new name. 如果这些值在您的模型上,而您只需要显示它们的总和,并且该总和仅对表示有意义,而对业务流程没有意义,那么这将是一个表示关注点,您可以在标记中使用转换器或,如果您关注的是MVVM,则可以将该摘要放在视图模型的新名称属性下。

If the sum of the two values are part of some business process, then that code should be in your domain and should return a model that already has the sum. 如果两个值的总和是某个业务流程的一部分,则该代码应在您的域中,并应返回已经具有总和的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM