Asp.net mvc MVC:我们是否应该考虑任何if-else或其他逻辑语句?
这是我的模型:Asp.net mvc MVC:我们是否应该考虑任何if-else或其他逻辑语句?,asp.net-mvc,asp.net-mvc-3,model-view-controller,separation-of-concerns,Asp.net Mvc,Asp.net Mvc 3,Model View Controller,Separation Of Concerns,这是我的模型: public class Employee { public string EmployeeName { get; set; } public string Address { get; set; } public DateTime DateOfBirth { get; set; } public int Salary { get; set; } } 我的看法是: <div> Employee Detail<br /&g
public class Employee
{
public string EmployeeName { get; set; }
public string Address { get; set; }
public DateTime DateOfBirth { get; set; }
public int Salary { get; set; }
}
我的看法是:
<div>
Employee Detail<br />
Employee Name : @Model.emp.EmployeeName<br />
Address : @Model.emp.Address <br />
Age : @Model.Age
<span style="color:@Model.SalaryColor">Salary : @Model.emp.Salary</span>
@if (Model.Salary > 20000)
{
<div1>..</div1>
}
else
{
<div2>..</div2>
}
</div>
员工详细信息
员工姓名:@Model.emp.EmployeeName
地址:@Model.emp.Address
年龄:@Model.Age
工资:@Model.emp.Salary
@如果(型号工资>20000)
{
..
}
其他的
{
..
}
使用这个if/else,我将在MVC的UI层/视图中插入业务逻辑。
但我不知道如何避免这种情况
我知道这会使UI与业务逻辑紧密结合,这是不好的。有什么办法可以避免这种情况吗?如果div1和div2的内容或多或少相似,最好是将If/else全部删除,或者在模型中增加一些字段 但是,如果两者完全不同,您可以使用if/else,这是可以接受的。但要注意什么是业务逻辑,什么不是业务逻辑。考虑:
public class Employee
{
public int Salary { get; set; }
private const int SALARY_CAP = 20000;
public bool IsSalaryAboveTheCap { get { return Salary > SALARY_CAP; } }
}
并且认为:
@if (Model.IsSalaryAboveTheCap)
{
<div1>..</div1>
}
else
{
<div2>..</div2>
}
@if(Model.issalaryoverthecap)
{
..
}
其他的
{
..
}
工资高于某个上限这一事实本身并不是商业逻辑。但是,上限的值为
可以说,视图模型并不是将这种逻辑放在其中的最佳位置。您是否有任何业务逻辑层可以从中获得这些信息?如果div1和div2的内容或多或少相似,最好是完全删除If/else,或者在模型中增加一些字段 但是,如果两者完全不同,您可以使用if/else,这是可以接受的。但要注意什么是业务逻辑,什么不是业务逻辑。考虑:
public class Employee
{
public int Salary { get; set; }
private const int SALARY_CAP = 20000;
public bool IsSalaryAboveTheCap { get { return Salary > SALARY_CAP; } }
}
并且认为:
@if (Model.IsSalaryAboveTheCap)
{
<div1>..</div1>
}
else
{
<div2>..</div2>
}
@if(Model.issalaryoverthecap)
{
..
}
其他的
{
..
}
工资高于某个上限这一事实本身并不是商业逻辑。但是,上限的值为
可以说,视图模型并不是将这种逻辑放在其中的最佳位置。您是否有任何业务逻辑层可以实现这一点?问题不在于
if
。有时,您必须根据模型的某些方面以不同的方式呈现事物。“问题”是工资>20000
的“逻辑”。虽然您给出的示例可能很简单,所以它并不十分重要,但理想情况下,这种事情并不真正属于视图
实现这一点的标准方法是使用。该对象的存在是为了将视图与数据模型的实现细节解耦,并封装一些表示层逻辑。如果您的数据模型是实体框架代理(Entity Framework proxy),则这一点尤其重要,因为实体框架代理具有许多约束和行为,这些约束和行为会迫使数据模型进入表示层的错误“形状”。在此特定实例中,您的ViewModel
可能具有一个与bool ShowHighSalarySection
类似的属性,该属性由ViewModel
计算,或由一些外部业务代码存储在ViewModel
中
ViewModel
的精确形状将在很大程度上取决于您所遵循的MV*的特定风格,但例如:-
if (Model.ShowHighSalarySection) { ...
var query = db.Employees.Select(x => ...);
// Some people like the "magic" of a mapper, especially when
// all you're doing is reasonably "dumb" transformations.
// Some people don't like that there could be somewhat complicated
// things going on behind the curtain.
// The huge advantage of this type of code is that you can usually
// leverage some kind of query projection and have the mapping
// take place on your database, rather than pulling the entire
// entity graph into memory.
var viewModel = this.mapper.Project()
.To<EmployeeViewModel>(query)
.FirstOrDefault();
return View(viewModel);
具体到哪里去取决于您的架构、您的哲学,以及您准备接受多少样板(或工具)来换取整洁ViewModel的主要功能是允许您将业务规则从视图中取出,放到适当的位置(更安全、更可测试、更可重用等等)。问题不在于
如果。有时,您必须根据模型的某些方面以不同的方式呈现事物。“问题”是工资>20000
的“逻辑”。虽然您给出的示例可能很简单,所以它并不十分重要,但理想情况下,这种事情并不真正属于视图
实现这一点的标准方法是使用。该对象的存在是为了将视图与数据模型的实现细节解耦,并封装一些表示层逻辑。如果您的数据模型是实体框架代理(Entity Framework proxy),则这一点尤其重要,因为实体框架代理具有许多约束和行为,这些约束和行为会迫使数据模型进入表示层的错误“形状”。在此特定实例中,您的ViewModel
可能具有一个与bool ShowHighSalarySection
类似的属性,该属性由ViewModel
计算,或由一些外部业务代码存储在ViewModel
中
ViewModel
的精确形状将在很大程度上取决于您所遵循的MV*的特定风格,但例如:-
带有数据模型构造函数的简单视图模型:-
在你看来:-
if (Model.ShowHighSalarySection) { ...
您还可以使用更“哑”的视图模型,以及映射器,例如:-
在控制器中:-
var query = db.Employees.Select(x => ...);
// Some people like the "magic" of a mapper, especially when
// all you're doing is reasonably "dumb" transformations.
// Some people don't like that there could be somewhat complicated
// things going on behind the curtain.
// The huge advantage of this type of code is that you can usually
// leverage some kind of query projection and have the mapping
// take place on your database, rather than pulling the entire
// entity graph into memory.
var viewModel = this.mapper.Project()
.To<EmployeeViewModel>(query)
.FirstOrDefault();
return View(viewModel);
具体到哪里去取决于您的架构、您的哲学,以及您准备接受多少样板(或工具)来换取整洁ViewModel的主要功能是允许您将业务规则从视图中取出,放到适当的位置(更安全、更可测试、更可重用等等)。简而言之。。。只要纯粹是为了表示,视图中的流控制就可以了。Angualr或Knockout适合这种类型的东西,您可以绑定Visible属性,所以您可以显示或隐藏绑定到属性的div。。。只要只是为了表示,视图中的流控制就可以了。Angualr或Knockout适合这种类型,您可以绑定Visible属性,这样就可以显示或隐藏绑定到属性的div