Nested ViewModel Classes in asp.net MVC

2019-08-09 11:32发布

问题:

I have four MVC model layer domain classes.

namespace MvcMobile.Models.BusinessObject
{
public class Speaker
{
    public int SpeakerID { get; set; }
    public string SpeakerName { get; set; }
}
public class Tag
{
    public int TagID { get; set; }
    public string TagName { get; set; }
}
public class Seminar
{
    public string Seminar_Code { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Room { get; set; }        
}
public class Seminar_Detail
{
    public string Seminar_Code { get; set; }
    public int SpeakerID { get; set; }
    public int TagID { get; set; }
    public string DateAndTime { get; set; } 
}
}

I would like to make CRUD operation by using these classes. So I create two VeiwModel Classes.

namespace MvcMobile.ViewModel
{
public class Seminar_Root_ViewModel
{
    public Seminar_Subsidiary_ViewModel Seminars { get; set; }
    public List<Speaker> Speakers { get; set; }
    public List<Tag> Tags { get; set; }
}
public class Seminar_Subsidiary_ViewModel
{
    public Seminar Seminar { get; set; }
    public List<Seminar_Detail> Seminar_Detail { get; set; }
}
}

For Controller layer, I consider that I will use Seminar_Root_ViewModel to make the whole CRUD operation processes.

What I would like to ask is that Is this proper way or correct way?
If you have more elegant way to make model layer and ViewModel layer, Please let me get suggestion.

Every suggestion will be appreciated.

[updated]

Let's assume that I make master-Detail form design.

Speaker and Tag are just look-up tables for dropdownlist or some controls like that.
Seminar is Master Data and Seminar_Detail will be Item Grid Data.
So As for this scenario, all of this classes are needed for this program.

Please let me know if my thinking is wrong.

回答1:

The only thing I can see is if you are not going to re-use your Seminar_Subsidiary_ViewModel view model you could skip it.

If you are going to need those two properties Seminar and Seminar_Detail on another view or ajax call, it's perfectly fine to have that kind of separation.

Personally I'm not a huge fan of _ on class name, but that have nothing to do with the question.