在ViewModel中复制实体(Replicate Entity inside ViewModel)

编程入门 行业动态 更新时间:2024-10-28 21:27:45
在ViewModel中复制实体(Replicate Entity inside ViewModel)

我有这个实体:

public class MyEntity : IMergeable<Entities.EntityExample> { [Required] public string Code { get; set; } [Required] public string Label{ get; set; } public System.DateTime DateStart { get; set; } }

我想在一个ViewModel中复制它,它将有一个名为DateEnd的第四个属性。 我在想这样做:

namespace MyNamespace.ViewModels { public class ExampleViewModel { public ExampleViewModel(MyEntity myEntity) { myentity = myEntity; } public myEntity myentity { get; set; } [Required] public string DateEnd { get; set; } } }

这有效,但我的问题是我的视图模型将按以下方式排列:

myViewModel.myentity.Code myViewModel.myentity.Label myViewModel.myentity.DateStart myViewModel.DateEnd

当我真正想要的是:

myViewModel.Code myViewModel.Label myViewModel.DateStart myViewModel.DateEnd

或者至少(不太好):

myViewModel.myentity.Code myViewModel.myentity.Label myViewModel.myentity.DateStart myViewModel.myentity.DateEnd

I have this entity:

public class MyEntity : IMergeable<Entities.EntityExample> { [Required] public string Code { get; set; } [Required] public string Label{ get; set; } public System.DateTime DateStart { get; set; } }

I want to replicate it in a ViewModel that will have a 4th property called DateEnd. I was thinking about doing this:

namespace MyNamespace.ViewModels { public class ExampleViewModel { public ExampleViewModel(MyEntity myEntity) { myentity = myEntity; } public myEntity myentity { get; set; } [Required] public string DateEnd { get; set; } } }

This works, but my problem is that my view model will be arranged in the following way:

myViewModel.myentity.Code myViewModel.myentity.Label myViewModel.myentity.DateStart myViewModel.DateEnd

When what I really wanted was:

myViewModel.Code myViewModel.Label myViewModel.DateStart myViewModel.DateEnd

Or at least (not so good):

myViewModel.myentity.Code myViewModel.myentity.Label myViewModel.myentity.DateStart myViewModel.myentity.DateEnd

最满意答案

您可以在视图模型上公开只访问基础实体属性的属性:

private myEntity _entity; public DateTime DateStart { get { return _entity.DateStart; } set { _entity.DateStart = value; } }

但我真的只是创建一个新的视图模型,然后使用像automapper或普通工厂这样的东西将视图模型转换为您的实体。 我想这是重复的,但实体 - >视图模型是我从未担心过的地方。

public class ViewModel { public string Code { get; set; } public string Label{ get; set; } public DateTime DateStart { get; set; } public DateTime DateEnd { get; set; } } var entity = new myEntity { Code = viewModel.Code, Label = viewModel.Label, DateStart = viewModel.DateStart ... dont know what you want to do with DateEnd }

但那你在哪里以及如何处理验证? 您在实体上有注释,因此您可以转换为实体,然后检查实体的有效性,但是您将失去对DateEnd的验证,因为它不是您实体的属性。 所以我会在视图模型上添加验证并在转换为实体之前检查其有效性。

You could expose properties on your view model that just accesses the underlying entity property:

private myEntity _entity; public DateTime DateStart { get { return _entity.DateStart; } set { _entity.DateStart = value; } }

But I would really just create a new view model and then use something like automapper or a plain factory to convert the view model to your entity. I guess this is repetitious but entity -> view model is the one place I've never worried about that.

public class ViewModel { public string Code { get; set; } public string Label{ get; set; } public DateTime DateStart { get; set; } public DateTime DateEnd { get; set; } } var entity = new myEntity { Code = viewModel.Code, Label = viewModel.Label, DateStart = viewModel.DateStart ... dont know what you want to do with DateEnd }

But then where and how do you handle validation? You have annotations on your entity so you could convert to the entity and then check the entity's validity but you'll lose validation on DateEnd since it's not a property on your entity. So I would add validation on the view model and check its validity before convert to an entity.

更多推荐

本文发布于:2023-04-27 20:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1328594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   ViewModel   Entity   Replicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!