【发布时间】:2017-07-16 05:42:08
【问题描述】:
我正在编写一个简单的 Web 应用程序,但遇到了一个架构问题。假设我的 BLL 层中有一个 Customer 类:
public class Customer
{
public int Id { get; set; }
public Person PersonalData { get; set; }
public int? MaximalPrice { get; set; }
public string Note { get; set; }
public Customer()
{
PersonalData = new Person();
}
}
我将把这个类的数据创建分为两个阶段。在第一阶段,用户将提供有关客户的基本信息,他将能够稍后完成。所以我在我的表示层中创建了两个视图。一个只显示基本信息,第二个。
基本:
<div class="input-field col s6">
<i class="material-icons prefix">phone</i>
@Html.TextBoxFor(m => m.TelephoneNumber, new {id = "icon_telephone", mask = "telephoneNumber"})
@Html.LabelFor(m => m.TelephoneNumber)
</div>
<div class="input-field col s6">
<i class="material-icons prefix">credit_card</i>
@Html.TextBoxFor(m => m.MaximalPrice, new { id = "credit_card" })
@Html.LabelFor(m => m.MaximalPrice)
</div>
</div>
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">note</i>
@Html.TextBoxFor(m => m.Note, new { id = "note_add" })
@Html.LabelFor(m => m.Note)
</div>
</div>
完整(部分视图):
<div class="row">
<div class="input-field col s6">
@Html.TextBoxFor(m => m.FirstName)
@Html.LabelFor(m => m.FirstName)
</div>
<div class="input-field col s6">
@Html.TextBoxFor(m => m.LastName)
@Html.LabelFor(m => m.LastName)
</div>
</div>
<div class="row">
<div class="input-field col s6">
@Html.TextBoxFor(m => m.Street)
@Html.LabelFor(m => m.Street)
</div>
<div class="input-field col s6">
@Html.TextBoxFor(m => m.City)
@Html.LabelFor(m => m.City)
</div>
</div>
<div class="row">
<div class="input-field col s6">
@Html.TextBoxFor(m => m.BirthDate)
@Html.LabelFor(m => m.BirthDate)
</div>
<div class="input-field col s6">
@Html.TextBoxFor(m => m.TelephoneNumber , new { mask = "telephoneNumber" })
@Html.LabelFor(m => m.TelephoneNumber)
</div>
</div>
现在我做了一个假设,当用户创建一个基本版本的客户时,他将无法在完整数据客户列表中看到它,当他完成客户数据时,他将无法在基本版本中看到它客户名单。 为了实现它,我正在考虑两种解决方案。
- 在 BLL 中创建函数,它将遍历我的类的所有属性,并检查是否有任何非基本属性已经不为空。
- 扩展客户的 DAL 域并添加将指示客户是“基本”还是“完整”的列
但以上解决方案都不是完美的。 First 在大数据集中的性能会很慢,因为迭代了所有客户条目的所有属性。所以这似乎是应该避免的。 我不喜欢第二种选择,这似乎解决了这个问题,因为它在表示层和数据访问层之间创建了依赖关系。我觉得在数据库中创建额外的另一列仅用于演示目的不是一个好主意。
所以,我的问题是:有没有更好的选择?您如何看待这个选项?你会如何解决这个问题?
【问题讨论】:
-
在数据库中添加一个新列,以确定客户是否是基本的,这不是一个坏主意。这将使您能够在许多其他地方(例如报告)过滤此类客户。其他选项是根据可以确定客户是否基本的任何其他列进行过滤。假设 Firstname 对于基本客户为空,但对于完整客户永远不会为空。总而言之,您应该根据某些条件过滤客户,而不是检查所有属性值。
-
谢谢,我想我会选择第二个选项。您如何看待将该逻辑放入表示层的基本客户模型视图中?
-
您应该在筛选记录的存储库层或数据访问层中具有该逻辑。您应该有单独的方法来检索基本客户和完整客户,并调用适当的方法来填充各自的视图模型。
标签: c# asp.net-mvc architecture