关于在 ViewModel 中复制属性的问题

发布于 2024-10-04 09:49:20 字数 2074 浏览 2 评论 0原文

我有一个关于视图模型中属性重复的问题。 对于我的搜索视图,我有一个如下所示的视图模型

public class SearchModel
{
    public IEnumerable<SelectListItem> Genders {get;set;}
    ... other select lists
    // Worker Details
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public DateTime Birthdate {get;set;}
    public int Phone {get;set;}et
    public string Gender {get; set;}

    //Address Details
    public string Street {get;set;}
    public string City {get;set;}
    public string Zip {get; set;}


}

对于我的输入视图,我有以下视图模型

  public IEnumerable<SelectListItem> Genders {get;set;}
  public IEnumerable<SelectListItem> Directions {get;set;}
    ... other select lists
    // Worker Details
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public DateTime Birthdate {get;set;}
    public int Phone {get;set;}et
    public string Gender {get; set;}

    public string SSN {get; set;}
    public string DL {get;set;}

    //Address Details
    public int Number {get;set;}
    public string Direction {get;set;}
    public string Suffix {get;set;}
    .....

    public string Street {get;set;}
    public string City {get;set;}
    public string Zip {get; set;}
}

列表显示模型

public class ListDisplayModel
{
   public IEnumerable<Worker> Workers {get;set;}

   internal class Worker 
   {
       public string FirstName {get;set;}
       public string LastName {get;set;}
       public DateTime Birthdate {get;set;}
       public int Phone {get;set;}et
       public string Gender {get; set;}

       public string SSN {get; set;}
       public string DL {get;set;}

       //Address Details
       public int Number {get;set;}
       public string Direction {get;set;}
       public string Suffix {get;set;}
       public string Street {get;set;}
       public string City {get;set;}
       public string Zip {get; set;}
   }
}

我觉得我正在复制很多属性。 我想知道对我来说,继续创建一个名为 Worker 的 DTO 类并将其放置在每个视图模型类中是否是理想的选择,或者是否有更好的方法来做这样的事情?

谢谢

I have a question regarding the duplication of properties within view models.
For my Search View i have a viewmodel that looks like this

public class SearchModel
{
    public IEnumerable<SelectListItem> Genders {get;set;}
    ... other select lists
    // Worker Details
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public DateTime Birthdate {get;set;}
    public int Phone {get;set;}et
    public string Gender {get; set;}

    //Address Details
    public string Street {get;set;}
    public string City {get;set;}
    public string Zip {get; set;}


}

For my Input View i have the following View Model

  public IEnumerable<SelectListItem> Genders {get;set;}
  public IEnumerable<SelectListItem> Directions {get;set;}
    ... other select lists
    // Worker Details
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public DateTime Birthdate {get;set;}
    public int Phone {get;set;}et
    public string Gender {get; set;}

    public string SSN {get; set;}
    public string DL {get;set;}

    //Address Details
    public int Number {get;set;}
    public string Direction {get;set;}
    public string Suffix {get;set;}
    .....

    public string Street {get;set;}
    public string City {get;set;}
    public string Zip {get; set;}
}

List Display Model

public class ListDisplayModel
{
   public IEnumerable<Worker> Workers {get;set;}

   internal class Worker 
   {
       public string FirstName {get;set;}
       public string LastName {get;set;}
       public DateTime Birthdate {get;set;}
       public int Phone {get;set;}et
       public string Gender {get; set;}

       public string SSN {get; set;}
       public string DL {get;set;}

       //Address Details
       public int Number {get;set;}
       public string Direction {get;set;}
       public string Suffix {get;set;}
       public string Street {get;set;}
       public string City {get;set;}
       public string Zip {get; set;}
   }
}

I feel like i'm duplicating a lot of properties.
I was wondering if it would be ideal for me to go ahead and create like a DTO class called worker and just place it in each of these view model classes or is there a better way to do something like this?

Thanks

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

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

发布评论

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

评论(2

傲鸠 2024-10-11 09:49:20

我会像您建议的那样在每个 ViewModel 中使用 WorkerViewModel 来改进代码并减少重复。

I'd go with the WorkerViewModel in each ViewModel like you suggested to improve the code and reduce duplication.

微凉 2024-10-11 09:49:20

绝对创建一个工作类并将这些属性放置在对象中。

Absoluetely create a worker class and place these properties in the object.

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