【问题标题】:Is this value object是这个值对象吗
【发布时间】:2012-07-29 18:07:08
【问题描述】:

有两个对象,人和图像。 Person 应该已将 id int 存储在 db 中,以便使用 src 属性正确呈现图像。

考虑以下场景

  1. 图片是使用网络表单 (mvc) 上传的
  2. Person 实体是使用 Web 表单 (mvc) 创建的,并且 javascript 用于检索选定的图像 id 以存储在 person 图像属性中
  3. 在视图方面,这些人物图像属性被加载到 img src 属性中以显示图像

你会如何设计 Person 对象

Person.cs
Id int not null
// To do

图像.cs

Id int not null
ImagePath string not null
AlternateText string 

【问题讨论】:

  • 没有足够的信息来确定。在做出答案之前,需要更多地了解该领域。应用程序中的人是否具有唯一性?做图片吗?没有人,图像是否有意义?没有图像的人有意义吗?
  • 为什么不在图片上使用外键绑定Person id。它允许您在一侧拥有一个只有 id 属性的 Person 类和一个 id 为 Person Id(外键)的图像类
  • 一般来说,如果你的对象有Id属性,不,它不是一个值对象。它是一个实体。

标签: c# asp.net-mvc domain-driven-design


【解决方案1】:
class PersonViewModel
{
    int Id {get; set;}
    int ImageId {get; set;
}

请注意,这是一个视图模型,专为您的 mvc 表示层量身定制。

【讨论】:

    【解决方案2】:

    假设您使用的是 EF 或类似的东西,您可以像这样设计您的类。

    public class Image
    {
        public int Id { get; set; }
        public string ImagePath { get; set; }
        public string AlternateText { get; set; }
        public int PersonId { get; set; }
    }
    
    public class Person
    {
        public int Id { get; set; }
    
        public List<Image> Images {
            get
            {
                return DbContext.Images.Where(image => image.PersonId == this.Id).ToList();
            }}
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 2017-11-22
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      相关资源
      最近更新 更多