asp.net MVC 中的嵌套 ViewModel 类
我有四个 MVC 模型层域类。
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; }
}
}
我想通过使用这些类进行 CRUD 操作。所以我创建了两个 VeiwModel
类。
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; }
}
}
对于Controller层
,我认为我会使用Seminar_Root_ViewModel
来制作整个CRUD操作流程。
我想问的是,这是正确的方法还是正确的方法?
如果您有更优雅的方式来制作模型层和ViewModel层
,请让我得到建议。
每一个建议都会受到赞赏。
[更新]
假设我进行主从表单设计。
Speaker 和 Tag
只是下拉列表或类似控件的查找表。Seminar
是主数据,Seminar_Detail
是项目网格数据。
所以对于这个场景,这个程序需要所有这些类。
如果我的想法是错误的,请告诉我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我唯一能看到的是,如果您不打算重新使用您的
Seminar_Subsidiary_ViewModel
视图模型,您可以跳过它。如果您需要在另一个视图或 ajax 调用中使用这两个属性
Seminar
和Seminar_Detail
,那么进行这种分离是完全可以的。就我个人而言,我不太喜欢类名上的 _ ,但这与问题无关。
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
andSeminar_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.