简体   繁体   English

ASP.Net MVC-正确执行MVVM

[英]ASP.Net MVC - doing MVVM correctly

I just have a quick question with regards to the MVVM design pattern... I'm just wondering if the way im doing things is the "correct" way? 我只是对MVVM设计模式有一个简单的问题……我只是想知道即时消息的处理方式是否是“正确”的方式?

I have a category mode like so: 我有一个类似的类别模式:

[Table("Category")]
public class CategoryModel
{
    [Key]
    public Int32 CategoryId { get; set; }
    public string CategoryName { get; set; }
    public Int32? Category_ParentID { get; set; }
    public virtual CategoryModel Parent_Category { get; set; }
    public virtual ICollection<CategoryModel> SubCategories { get; set; }
    public Int16 CategoryOrder { get; set; }
    public bool Deleted { get; set; }

}

Not all of these are need on the "Create New Category" form, so I've created a ViewModel with just the required fields: 在“创建新类别”表单上并不是所有这些都需要,所以我创建了一个ViewModel,其中仅包含必填字段:

public class CategoryViewModel
{
    public string CategoryName { get; set; }           
    public Int32? Category_ParentID { get; set; }       
}

When a new category is being created, I send in the ViewModel as a parement, and create a new CategoryModel using the values from the ViewModel, like so: 当创建一个新类别时,我将ViewModel作为部件发送,并使用ViewModel中的值创建一个新的CategoryModel,如下所示:

if (ModelState.IsValid)
{

    CategoryModel newCategory = new CategoryModel()
    {
    CategoryName = model.CategoryName,
    Category_ParentID = model.Category_ParentID
    };

    //get the root node, and include it's subcategories
    var categoryList = DatabaseContext.Categories.Include("SubCategories").Single(c => c.CategoryId == model.Category_ParentID);

    //set the order of the category by getting the number of subcategories
    if (categoryList.SubCategories.Count > 0)
    newCategory.CategoryOrder = (Int16)(categoryList.SubCategories.Where(c => !c.Deleted).Max(c => c.CategoryOrder) + 1);
    else
    newCategory.CategoryOrder = 1;

    //save the new category
    DatabaseContext.Categories.Add(newCategory);
    await DatabaseContext.SaveChangesAsync();

    return PartialView("_CategoryModelItem", model);

} else {
    return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}

Is this the right way to do the MVVM design pattern (ie the values are taken from a ViewModel, a new Model is created - takes values from the ViewModel - and gets written to the DB)? 这是执行MVVM设计模式的正确方法(即,从ViewModel中获取值,创建新模型-从ViewModel中获取值-并将其写入数据库)吗? or am I misunderstanding how its suppored to be done? 还是我误解了该如何完成?

Also

How does the "viewing" part work? “查看”部分如何工作? What I mean is, if I'm listing a category and all of its subcategories in the Index() method of the Controller... should I used a model or viewmodel to display them? 我的意思是,如果我要在Controller的Index()方法中列出一个类别及其所有子类别...我应该使用模型还是视图模型来显示它们? Of so, I'll need to add need to add the ICollection to the ViewModel, and then should I grab the data using Entity Framework and the Model class, and then somehow feed that into a ViewModel? 因此,我需要添加将ICollection添加到ViewModel的需求,然后我应该使用Entity Framework和Model类获取数据,然后以某种方式将其馈入ViewModel吗?

Yes, this is correct. 是的,这是正确的。 But I'd make use of something like automapper to reduce the actual mapping code you need to write. 但是我会使用automapper之类的东西来减少您需要编写的实际映射代码。

You'll use a viewmodel for displaying the items and it will be mapped from your model (again made easier with automapper). 您将使用viewmodel显示项目,并将其从模型中进行映射(再次使用automapper使之更容易)。

You would also place your dataannotations on the viewmodel, and that will help with the validation logic on your view. 您还可以将数据注释放在视图模型上,这将有助于视图上的验证逻辑。

However, you don't need to use MVVM when you are doing MVC. 但是,在执行MVC时不需要使用MVVM。 They are different patterns that happen to be able to work together. 它们是碰巧能够一起工作的不同模式。 MVVM is more commonly used in WPF application development. MVVM在WPF应用程序开发中更常用。

Your are correct but i have some other tips for you: 您是正确的,但我还有其他提示:

  • do not name Entity Framework entities with Model postfix, use the names of your DB tables in singular form 不要使用Model后缀来命名Entity Framework实体,请使用单数形式的数据库表名称
  • always return a object. 总是返回一个对象。 If you want to return a list of objects then wrap this list into a class. 如果要返回对象列表,则将此列表包装到一个类中。 It will help you to keep your ViewModels more flexible and easy to extend. 这将帮助您使ViewModels更灵活和易于扩展。 Use the wrapper class also if you want to return a single value. 如果要返回单个值,请也使用包装器类。 I call these objects PageModels. 我称这些对象为PageModels。

If i want to return a single category then my edit page model will look like: 如果我想返回一个类别,那么我的编辑页面模型将如下所示:

public class CategoryEditPageModel
{    
    CategoryModel Category { get; set;}
    //other data not directly related to category itself but needs to be visible on the page
    User CategoryOwner { get; set; }
}

Then POST only the CategoryModel if you want to update a category. 如果要更新类别,则仅发布CategoryModel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM