的ViewModels,CQRS和实体 [英] ViewModels, CQRS and Entities

查看:125
本文介绍了的ViewModels,CQRS和实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何可能利用视图模型,一个命令和数据库实体

I am trying to understand how to probably make use of a view model, a command and the database entity

目前,我觉得有很多这样的手动映射的,而且我不确定我是否应该使用类似AutoMapper工具地图视图模型< - >命令< - >当实体有很多属性(比如10-15)。

At the moment I think there is a lot of manual mapping between this, and I am unsure if I should use a tool like AutoMapper to map ViewModel <-> Command <-> Entity when there is a lot of properties (like 10-15).

拿这个例子(在记事本中写的快,可能无法编译 - 在我真正的应用程序了我使用依赖注入和IOC):

Take this example (written quickly in notepad, may not compile - in my real applicaiton I use dependency injection and IoC):

public class Student {
    public int Id { get; set; }
    public string Name { get; set; }

    public string AnoterProperty { get; set; }
    public string AnoterProperty2 { get; set; }     
    public string AnoterProperty3 { get; set; }    
    public string AnoterProperty4 { get; set; }    
    public string AnoterProperty5 { get; set; }    

    public int StudentTypeId { get; set; }
    public StudentType StudentType { get; set; } // one-to-many
}

public class CreateStudentViewModel {
    public string Name { get; set; }
    public DropDownList StudentTypes { get; set; }
}

public class DropDownList {
    public string SelectedValue { get; set; }
    public IList<SelectListItem> Items { get; set; }
}

public class CreateStudent {
    public string Name { get; set; }
    public int StudentTypeId { get; set; }
}

public class HandleCreateStudentCommand {

    public void Execute(CreateStudent command) {
        var student = new Student {
            Name = command.Name,
            StudentTypeId = command.StudentTypeId
        };

        // add to database
    }

}

public class StudentController {

    public ActionResult Create() {
        var model = new CreateStudentViewModel {
            StudentTypes = new DropDownList { 
               Items = // fetch student types from database 
            }
        };

        return View(model);
    }

    public ActionResult Create(CreateStudentViewModel model) {
        var command = new CreateStudent {
            Name = model.Name,
            StudentTypeId = Convert.ToInt32(model.Items.SelectedValue);
        };

        var commandHandler = new HandleCreateStudentCommand();
        commandHandler.Execute(command);
    }

}

在这里是什么我担心的是,我做了很多的不同部分之间手动映射。而这个例子只包含一些属性。

What worries me here is that I do a lot of manual mapping between the different parts. And this example only contains a few properties.

我对其中最有可能包含学生实体的所有可能的属性的可能更新命令特别担心。

I am especially worried about a possible update command which will most likely contain all possible properties of the student entity.

有没有很好地解决,或者我应该去与AutoMapper和地图从视图模型&LT; - &GT;命令命令&lt; - &GT;实体

Is there a neat solution, or should I go with AutoMapper and map from ViewModel <-> Command and Command <-> Entity?

推荐答案

一种可能的方式来处理更少的类和映射/预测/转换:

One possible way to deal with less classes and mappings / projections / conversions:

有关中使用的所有视图的写面的应用程序,让命令是他们的模型(查看模型)(即允许用户提交表单左右视图)。

For all the views used in the WRITE-SIDE of your application (the views that allow the user to submit a form or so), let the Command to be their Model (View Model).

也就是说,你可以有:

[HttpPost]
public ActionResult Create(CreateStudent command) {
    commandHandler.Execute(command);
}

对于付诸行动的,我看到你有填充一个下拉列表...

As for the get action, I see that you have to populate a drop-down list...

[HttpGet]
public ActionResult Create() {
    // var model = create the view model somehow
    return View(model);
}

现在,作为后者片段的模式,你可能有这些选项(可能还有其他):

Now, as for the model in the latter snippet, you may have these options (and perhaps others):


  • 让Command对象(CreateStudent)是视图模型(作为视图是一个...查看了命令,请求视图),并使用传入的下拉列表项的 ViewBag

  • 从CreateStudent命令派生只是为了它也可以保持下拉项目视图模型

请注意,有没有必要后缀命令添加到您的命令对象。只要给它一个名字,这意味着的 DoSomething的的 - 动词短语(动词+宾语) - 它应该是罚款

Notice that there is no need to add the suffix "Command" to your command object. Just give it a name that means doSomething - a verb phrase (verb + object) - and it should be fine.

在年底,约命令的对象真的是有些看法的机型 - 无须定义另一个视图模型的,然后有很多的重复等等

In the end, some command objects really are the models for some views - no need to define another view-models for those and then have a lot of duplication etc.

此外,您可能会发现这些有趣的:

Also, you may find these interesting:

  • http://cqrs.nu/Faq
  • http://cqrsjourney.github.io/
  • https://github.com/mspnp/cqrs-journey-code/tree/master/source/Conference

这篇关于的ViewModels,CQRS和实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆