直接绑定域模型是一个坏主意吗?

发布于 2024-10-18 21:40:20 字数 150 浏览 3 评论 0原文

我很好奇将模型直接绑定为操作方法中的参数是否被认为是坏主意?

如果表单变得复杂,我可以创建一个自定义模型绑定器。

使用这种方法有什么陷阱吗?

我想避免创建视图模型,因为我想让我的应用程序尽可能简单。我想避免将代码和模型视图复制到模型绑定。

I'm curious if binding models directly as parameters in the action method is considered as bad idea ?

If the form gets to complex, I could create a custom model binder.

Are there any pitfals using this approach ?

I'd like to avoid creating of viewmodel, because I want to keep me app as simple as possible. I'd like to avoid of duplicating the code and the modelview to model binding.

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

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

发布评论

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

评论(3

梦中的蝴蝶 2024-10-25 21:40:20

未必。但很快您就会发现模型上需要一些状态,但它是特定于 UI 的,无论如何您最终都会创建一个 ViewModel。

另外,对于某些属性(例如 DateTime),如果定义为 DateTime,则在模型上效果不佳,因为使其可选会出现问题,因此您必须将其定义为字符串。我不相信你想用 string 来表示日期。

此外,对于 DropDown 项目,您需要在模型上有一个用于 SelectListItem 的集合,这在模型上没有意义。

这就是发生在我身上的事。

Not necessarily. But soon you will find some state which needs to be on the model but is UI specific and you end up creating a ViewModel anyway.

Also for some properties such as DateTime, this would not work well on the Model if defined as DateTime since there is a problem with making it optional so you have to define it as string. And I do not believe you wanna put string for a date.

Additionally, for DropDown items you need to have a collection on the Model for the SelectListItem which does not make sense on the Model.

That's what happened to me.

[旋木] 2024-10-25 21:40:20

我建议几乎总是使用视图模型。

如果您使用默认的对象模板...它们并不真正喜欢非平面模型,虽然您可以覆盖它们,但这并不总是一个好主意。领域模型通常不是平坦的。无论哪种方式,任何基于 ModelMetaData 的东西使用 ViewModel 都会变得更容易。

使用 ViewModel 进行验证也更容易,因为您有一个更有针对性的模型,有时验证仅在某些视图中有意义。

创建 ViewModel 比使用 JsonResult 发送模型更好、更安全......好吧......你永远不应该向外部发送域模型,即使你不使用 ViewModel。但是,当您准备好 ViewModel 时,使用 JsonResult 会更容易。当您习惯于在任何地方使用域模型时,也更容易犯错误并暴露敏感信息。

更改有时更容易,因为更改域模型上的属性并不意味着您必须更改所有视图,只需更改 ViewModel 的创建(如果您使用某种映射器,这只是对绑定的一个更改) ),尽管在我看来这不是一个非常强的点。

其他一些好处是将表示层与业务层解耦,如果您使用 EF 或非 poco 对象,则在视图中使用它们会更困难。

如果您想消除代码重复,您可以考虑创建自动创建 ViewModel 的过滤器,即使没有操作过滤器,使用映射器也可以消除大量代码重复。

顺便说一句,我不明白创建自定义模型绑定器比使用 ViewModel 更简单。

I'd advise to almost always use view models.

If you're using the default object templates... they don't really like non-flat models, and while you could override them, it's not always a good idea. Domain Models are usually not flat. Either way, anything based on ModelMetaData is easier with a ViewModel.

Validation is also easier with a ViewModel, as you've got a more focused model, and sometimes there's validation that only makes sense in some views.

Creating ViewModels is much better and safer then sending models using JsonResult... Well... you should NEVER send Domain Models outside anyway, even if you're not using ViewModels. But it's easier using JsonResult when you've got a ViewModel ready. It's also easier to make mistakes and expose sensitive information when you're used to using your domain models everywhere.

Changes are sometimes easier, because changing a property on the Domain Model doesn't mean you have to change all your views, just change the creation of the ViewModel (if you're using some kind of mapper, that's just one change to the binding), although this isn't a very strong point IMO.

Some other benefits are decoupling the presentation layer from the business layer, and if you're using EF or non-poco objects, it'll be harder to use them in views.

If you want to eliminate duplication of code, you could consider creating filters that automatically create your ViewModels, and even without action filters using a mapper does eliminate a lot of code duplication.

BTW, I don't see how creating a custom model binder is simpler than using ViewModels.

澉约 2024-10-25 21:40:20

我建议您始终创建一个 ViewModel。在最简单的形式中,它可能只包含具有域对象(和附件数据)的属性。比如:

public class DomainModelClass
{
    int Property1 { get; set; }
    int Property2 { get; set; }
}

public class ViewModelClass
{
    DomainModelClass DomainModel { get; set;}

    SelecList List1 { get; set; }
}

无论如何,这个建议只是为了让事情变得简单,因为我建议你创建一个“真正的”视图模型,并使用像 AutoMapper 这样的东西来映射 ViewModel <-> DomainModel,即使在简单的场景中也是如此。

I would advise you to always create a ViewModel. In its simplest form it might simply contain a property with the domain object (and accessory data). Something like:

public class DomainModelClass
{
    int Property1 { get; set; }
    int Property2 { get; set; }
}

public class ViewModelClass
{
    DomainModelClass DomainModel { get; set;}

    SelecList List1 { get; set; }
}

Anyway, this suggestion is just to keep things simple, because I would advise you to create a "real" viewmodel, and use something like AutoMapper to map the ViewModel <-> DomainModel, even in a simple scenario.

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