C# 在数据模型类的属性中包含一些逻辑代码可以吗?
我正在学习MVC3,我还没有发现有人在数据模型类的属性中使用一些逻辑代码 他们按如下方式创建数据模型类(例如): 在属性中包含如下逻辑代码可以吗C# 在数据模型类的属性中包含一些逻辑代码可以吗?,c#,asp.net-mvc,C#,Asp.net Mvc,我正在学习MVC3,我还没有发现有人在数据模型类的属性中使用一些逻辑代码 他们按如下方式创建数据模型类(例如): 在属性中包含如下逻辑代码可以吗 public class Customer { private int customerId; public int CustomerId { get{return customerId;} set { customerId=value; // some lo
public class Customer
{
private int customerId;
public int CustomerId {
get{return customerId;}
set
{
customerId=value;
// some logic codes go here.
}
}
//other properties go here.
}
编辑1: 这是我的真实场景: 子表数据模型:
namespace MvcApplication1.Models
{
public class Choice
{
public int ChoiceId { get; set; }
public string Description { get; set; }
public bool IsCorrect { get; set; }
public QuizItem QuizItem { get; set; }
}
}
namespace MvcApplication1.Models
{
public class QuizItem
{
public int QuizItemId { get; set; }
public string Question { get; set; }
private IEnumerable<Choice> choices;
public IEnumerable<Choice> Choices
{
get { return choices; }
set
{
choices = value;
foreach (var x in choices)
x.QuizItem = this;
}
}
}
}
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var data = new List<QuizItem>{
new QuizItem
{
QuizItemId = 1,
Question = "What color is your hair?",
Choices = new Choice[]{
new Choice{ ChoiceId=1, Description="Black.", IsCorrect=true},
new Choice{ ChoiceId=2, Description="Red.", IsCorrect=false},
new Choice{ ChoiceId=3, Description="Yellow.", IsCorrect=false}
}
},
new QuizItem
{
QuizItemId = 2,
Question = "What color is your noze?",
Choices = new Choice[]{
new Choice{ChoiceId=1, Description="Pink.", IsCorrect=false},
new Choice{ChoiceId=2, Description="Maroon.", IsCorrect=true},
new Choice{ChoiceId=3, Description="Navy Blue.", IsCorrect=false}
}
}
};
return View(data);
}
}
}
父表数据模型:
namespace MvcApplication1.Models
{
public class Choice
{
public int ChoiceId { get; set; }
public string Description { get; set; }
public bool IsCorrect { get; set; }
public QuizItem QuizItem { get; set; }
}
}
namespace MvcApplication1.Models
{
public class QuizItem
{
public int QuizItemId { get; set; }
public string Question { get; set; }
private IEnumerable<Choice> choices;
public IEnumerable<Choice> Choices
{
get { return choices; }
set
{
choices = value;
foreach (var x in choices)
x.QuizItem = this;
}
}
}
}
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var data = new List<QuizItem>{
new QuizItem
{
QuizItemId = 1,
Question = "What color is your hair?",
Choices = new Choice[]{
new Choice{ ChoiceId=1, Description="Black.", IsCorrect=true},
new Choice{ ChoiceId=2, Description="Red.", IsCorrect=false},
new Choice{ ChoiceId=3, Description="Yellow.", IsCorrect=false}
}
},
new QuizItem
{
QuizItemId = 2,
Question = "What color is your noze?",
Choices = new Choice[]{
new Choice{ChoiceId=1, Description="Pink.", IsCorrect=false},
new Choice{ChoiceId=2, Description="Maroon.", IsCorrect=true},
new Choice{ChoiceId=3, Description="Navy Blue.", IsCorrect=false}
}
}
};
return View(data);
}
}
}
namespace mvcapapplication1.Models
{
公共类QuizItem
{
public int QuizItemId{get;set;}
公共字符串问题{get;set;}
私人的无数选择;
公共数字选择
{
获取{返回选项;}
设置
{
选择=价值;
foreach(选项中的变量x)
x、 QuizItem=这个;
}
}
}
}
消费者:
namespace MvcApplication1.Models
{
public class Choice
{
public int ChoiceId { get; set; }
public string Description { get; set; }
public bool IsCorrect { get; set; }
public QuizItem QuizItem { get; set; }
}
}
namespace MvcApplication1.Models
{
public class QuizItem
{
public int QuizItemId { get; set; }
public string Question { get; set; }
private IEnumerable<Choice> choices;
public IEnumerable<Choice> Choices
{
get { return choices; }
set
{
choices = value;
foreach (var x in choices)
x.QuizItem = this;
}
}
}
}
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var data = new List<QuizItem>{
new QuizItem
{
QuizItemId = 1,
Question = "What color is your hair?",
Choices = new Choice[]{
new Choice{ ChoiceId=1, Description="Black.", IsCorrect=true},
new Choice{ ChoiceId=2, Description="Red.", IsCorrect=false},
new Choice{ ChoiceId=3, Description="Yellow.", IsCorrect=false}
}
},
new QuizItem
{
QuizItemId = 2,
Question = "What color is your noze?",
Choices = new Choice[]{
new Choice{ChoiceId=1, Description="Pink.", IsCorrect=false},
new Choice{ChoiceId=2, Description="Maroon.", IsCorrect=true},
new Choice{ChoiceId=3, Description="Navy Blue.", IsCorrect=false}
}
}
};
return View(data);
}
}
}
namespace mvcapapplication1.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
var数据=新列表{
新奎兹坦
{
QuizItemId=1,
问=“你的头发是什么颜色的?”,
选择=新选择[]{
新选项{ChoiceId=1,Description=“Black.”,IsCorrect=true},
新选项{ChoiceId=2,Description=“Red.”,IsCorrect=false},
新选项{ChoiceId=3,Description=“Yellow.”,IsCorrect=false}
}
},
新奎兹坦
{
QuizItemId=2,
问=“你的睡衣是什么颜色的?”,
选择=新选择[]{
新选项{ChoiceId=1,Description=“Pink.”,IsCorrect=false},
新选项{ChoiceId=2,Description=“Maroon.”,IsCorrect=true},
新选项{ChoiceId=3,Description=“海军蓝。”,IsCorrect=false}
}
}
};
返回视图(数据);
}
}
}
这需要一个方法。原因有二:
- 我不建议集合使用setter
- -每次设置属性时,为集合中的每个项目设置属性是昂贵的,不应在属性中。相反,首选一种方法
- setter中的代码(在您的案例中)会导致足够多的副作用,从而取消该属性的使用资格
- -关于集合设置器的StackOverflow讨论
public class QuizItem
{
public int QuizItemId { get; set; }
public string Question { get; set; }
private IEnumerable<Choice> choices;
public IEnumerable<Choice> Choices
{
get { return choices; }
}
public void SetChoices(IEnumerable<Choice> choices)
{
foreach (var x in choices)
x.QuizItem = this;
this.choices = choices;
}
}
公共类QuizItem
{
public int QuizItemId{get;set;}
公共字符串问题{get;set;}
私人的无数选择;
公共数字选择
{
获取{返回选项;}
}
公共void集合选项(IEnumerable选项)
{
foreach(选项中的变量x)
x、 QuizItem=这个;
这个。选择=选择;
}
}
我认为这个逻辑应该在控制器中实现。然而,我总是在我的模型中定义POCO类,并使用ViewModel来实现这种简单的逻辑。这更像是一种哲学方法。因此,这取决于一场辩论
目前为止,最流行的方法是使用严格分层的关注点分离方法,其中“模型”对象只负责包含数据,如果您想在上面应用任何类型的业务逻辑,则需要在单独的“业务逻辑”层上实现,它处理诸如数据完整性验证/验证、根据业务流程对数据进行变异等问题的应用
另一种方法是使用模型层对目标域的业务进行实际建模(如动词)。在这种情况下,模型充当业务规则的直接定义,并且应该像业务规则所要求的那样丰富。(这种方法已被采用到极致,基本上在模型中保留数据结构和业务逻辑,并从同一模型生成ORM、控制器逻辑和视图)
通常,“我的模型对象可以/应该有多聪明”是您使用的框架提出的问题。有些框架根本不在乎这两种方式(ASP.NET MVC),其他框架则希望您永远不要担心编码这些东西,只要您提供足够的元数据,以便他们可以为您完成工作(NHibernate,Entity Framework)。还有一些人鼓励您在域对象模型(例如裸对象)中表达所有业务规则和逻辑。在我看来,数据模型应该执行与数据(值)相关的逻辑,如“此值对…有效吗?”。此外,在执行隐藏逻辑(如本例中的“附加父对象”)时,将方法命名为“set”也是错误的 更复杂的数据模型示例:
您在setter中做什么工作?在我看来,在属性setter中有合理的副作用是可以的。例如,引发一个
PropertyChanged
事件是可以接受的,new NuclearMissile().Launch()
不是。@alexn:我将更新我的问题以代表真实的场景。您能否在上面的项目符号列表中对您的两个原因进行更详细的解释?