将模型读入 ViewModel 以及 MVVM 中的其他方式 - 最佳实践 -

发布于 2024-09-18 03:27:36 字数 199 浏览 3 评论 0原文

我想深入了解您的日常工作:P

您如何将 Person 数据读取到 PersonViewModel 中?

它只是一个

PersonViewModel pVM = staticHelper.ConvertPersonToPersonViewModel(person);

还是有更酷的东西?

I would like to get an insight into your daily work :P

How do you read the Person data into the PersonViewModel ?

Is it just a

PersonViewModel pVM = staticHelper.ConvertPersonToPersonViewModel(person);

or is there something cooler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨落□心尘 2024-09-25 03:27:36

只需将 Person 对象包含在视图模型中,根本不要尝试复制该对象。

public class PersonViewModel 
{
    public Person Person { get; set; }

    ... plus other properties your view model might need
}

然后在你的控制器中:

PersonViewModel pVM = new PersonViewModel { Person = person } ;

Simply include the Person object in the view model, don't try to copy the object at all.

public class PersonViewModel 
{
    public Person Person { get; set; }

    ... plus other properties your view model might need
}

then in your controller:

PersonViewModel pVM = new PersonViewModel { Person = person } ;
◇流星雨 2024-09-25 03:27:36

Automapper 是自 for 循环以来最好的东西,甚至可能是 if 语句。

Automapper is the best thing since the for loop, maybe even the if statement.

情话已封尘 2024-09-25 03:27:36

有些人提倡复制构造函数

其他人可能使用反射复制属性

当然,没有任何内容表明您不能在复制构造函数中使用反射来复制属性。

Some folks advocate copy constructors.

Others might use reflection to copy properties.

Of course, nothing says you can't use reflection to copy properties while in a copy constructor.

離殇 2024-09-25 03:27:36

我认为您误解了视图模型的观点。
视图模型应该是模型的映射/接口,而不是模型的副本。

I think you're misunderstanding the point of the viewmodel.
The viewmodel is supposed to be a mapping / interface to the model, not a copy of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文