ASP.Net MVC2 - 使用 DataAnnotations 从数据库对象的值中检索设置 ViewModel 值

发布于 2024-10-10 21:48:08 字数 1318 浏览 1 评论 0原文

在使用 ViewModel 之前,我可以轻松地将“即将编辑”对象直接传递到视图,而无需大惊小怪地设置各个属性等,因为视图可以方便地直接接受 Employee 类型。

    [HttpGet]
    public ActionResult EditEmployee(int? id)
    {
        EmployeeRepository ER = new EmployeeRepository();
        Employee SomeEmployee = ER.GetEmployee(id.Value);

        if(SomeEmployee!=null)
            return View(SomeEmployee);

但现在我正在使用 ViewModel将 DataAnnotations 属性应用于各种属性的顶部以进行验证。这就产生了一个问题。

从数据库中获取“即将编辑”的对象后,设置 ViewModel 的值突然变得更加复杂。我不能简单地将检索到的对象直接传递给视图,因为视图现在需要 VMEmployee 类型。

我希望能够执行以下操作:

    [HttpGet]
    public ActionResult EditEmployee(int? id)
    {
        EmployeeRepository ER = new EmployeeRepository();
        Employee SomeEmployee = ER.GetEmployee(id.Value);

        if(SomeEmployee!=null)
            return View(new VMEmployee(SomeEmployee));

所有路径似乎都通向一个巨大的构造函数,该构造函数手动设置每个单独属性的值。但以前当我不使用 ViewModel 时,我从来不需要这样做。模型绑定是一件幸事!

我的对象也有复杂的子对象,我的表单也在为其收集值,因此这对于 DRY 主体来说将是一项巨大/冗长的任务。

我什至不想使用 ViewModel,但不得不这样做,因为我需要两个不同的 DataAnnotations 规则集来应用于同一对象的不同验证场景。

我想要做的就是能够针对不同的场景有两个不同的 DataAnnotations 规则集。即面向公众的 www 站点与面向内部的管理站点。 DataAnnotations 似乎不够灵活,无法轻松满足这种常见需求。

我尝试过 AutoMapper,但它抛出一个错误,指出它无法映射我的对象类型,我怀疑是因为 Employee 是由 LINQ to SQL 自动生成的。

在坚持 DRY 原则的同时实现这一目标的最优雅的方法是什么?

Prior to using a ViewModel, I could easily pass the "soon to be edited" object directly to the view without needing to fuss over setting individual properties etc as the View conveniently accepted the Employee type directly..

    [HttpGet]
    public ActionResult EditEmployee(int? id)
    {
        EmployeeRepository ER = new EmployeeRepository();
        Employee SomeEmployee = ER.GetEmployee(id.Value);

        if(SomeEmployee!=null)
            return View(SomeEmployee);

But now I'm using a ViewModel with DataAnnotations attributes applied over the top of various properties for validation purposes. Which creates a problem..

After fetching the "soon to be edited" object from the db, setting the ViewModel's values is suddenly a whole lot more complicated. I can't simply pass the retrieved object straight to the view, as the View now expects the VMEmployee type instead.

I would like to be able to do something like:

    [HttpGet]
    public ActionResult EditEmployee(int? id)
    {
        EmployeeRepository ER = new EmployeeRepository();
        Employee SomeEmployee = ER.GetEmployee(id.Value);

        if(SomeEmployee!=null)
            return View(new VMEmployee(SomeEmployee));

All paths seem to lead to a huge constructor which manually sets the values of each individual property. But I never had to do that before when I wasn't using a ViewModel. Model binding was a blessing!

My objects also have complex child objects, which my form is also collecting values for, so this would be a huge/verbose task against DRY principals.

I don't even really want to use a ViewModel, but am forced to because I need two different DataAnnotations rule sets for different validation scenarios applied to the same object.

All I want to do is be able to have two different DataAnnotations rule sets for different scenarios. I.e. public-facing www site vs internal-facing admin site. DataAnnotations doesn't seem to be flexible enough to easily cater for this common need.

I've tried AutoMapper, but it throws an error saying it can't map my object types, I suspect because Employee was auto-generated by LINQ to SQL.

What is the most elegant way to achieve this while sticking to DRY principals?

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

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

发布评论

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

评论(1

清眉祭 2024-10-17 21:48:08

我将定义一个映射类,其唯一目的是将 Employee 映射到 VMEmployee。映射仍然需要存在,但理想情况下视图模型不需要关心它。然后您可以对此进行单元测试以确保所有内容都已映射。

为了促进这一点,您可以考虑使用诸如 Automapper 之类的东西。然而,我倾向于发现手动编写映射代码最终会更容易。

WhoCanHelpMe 给出了如何使用 Automapper 使用合适的 OOP 结构和 IOC 来实现这一点的示例。

I would define a mapping class where its sole purpose will be to map an Employee to a VMEmployee. The mapping will still need to exist but ideally the viewmodel should not need to be concerned with doing it. You can then unit test this to make sure that everything is mapped.

To facilitate this you could look into using something like Automapper. However, I tend find that manually writing mapping code ends up being easier.

The WhoCanHelpMe gives an example of how to implement this with Automapper using a decent OOP structure and IOC.

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