是这个价值对象

时间:2012-07-29 18:07:09

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

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

考虑以下方案

  1. 使用网络表单(mvc)上传图片
  2. 使用网络表单(mvc)创建人物实体,并使用javascript检索选定的图像ID以存储在人物图像属性内
  3. 在视图方面,这些人物图像属性被加载到img src attrubute中以显示图像
  4. 你如何设计Person对象

    Person.cs
    Id int not null
    // To do
    

    Image.cs

    Id int not null
    ImagePath string not null
    AlternateText string 
    

2 个答案:

答案 0 :(得分:2)

class PersonViewModel
{
    int Id {get; set;}
    int ImageId {get; set;
}

请注意,这是一个视图模型,尤其适合您的mvc表示层。

答案 1 :(得分:0)

假设您正在使用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();
        }}

}