C#将子对象传递给要填充基类公共属性的方法

时间:2016-12-04 13:54:03

标签: c# inheritance reference polymorphism

我有一个基本实体类,它由两个类派生,名为Student和Department。

public abstract class Entity
{
    [Key]
    public string Id { get; set; }
    [Required]
    public DateTime Created { get; set; }
    [Required]
    public string CreatedBy { get; set; }
    [Required]
    public DateTime Modified { get; set; }
    [Required]
    public string ModifiedBy { get; set; }       
}

 public class Department : Entity
{
    [Required]
    [Index]
    [StringLength(128)]
    public string Name { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

 public class Student : Entity
    {
        [Required]
        [Index]
        [StringLength(128)]
        public string Name { get; set; }
        public string Phone { get; set; }

        [Required]
        public string DepartmentId { get; set; }
        [ForeignKey("DepartmentId")]
        public virtual Department Department { get; set; }        
    }

现在我想实例化派生类,但不想复制常用的初始化代码。我已经使用了ref等方法,但它正在构建错误。

    public Student CreateStudent()
    {
        Student student = new Student
        {
            Name = nameTextBox.Text,
            Phone = phoneTextBox.Text,
            DepartmentId = departmentComboBox.SelectedValue.ToString()
        };
        SetCommonValues(ref student);
        return student;
    }

    public Department CreateDepartment()
    {
        Department department=new Department()
        {
            Name = nameTextBox.Text
        };
        SetCommonValues(ref department);
        return department;
    }

    public void SetCommonValues(ref Entity entity)
    {
        entity.Id = Guid.NewGuid().ToString();
        entity.Created = DateTime.Now;
        entity.Modified = DateTime.Now;
        entity.CreatedBy = Constants.UserName;
        entity.ModifiedBy = Constants.UserName;            
    }

任何建议都将受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

我可能在这里遗漏了一些东西。您是否有任何特殊原因选择抽象类中的构造函数,如下所示?

public abstract class Entity
{
    public string Id { get; set; }
    public DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public DateTime Modified { get; set; }
    public string ModifiedBy { get; set; }

    public Entity()
    {
        this.Id = Guid.NewGuid().ToString();
        this.Created = DateTime.UtcNow;
        this.Modified = this.Created;
    }

    public Entity(string createdBy, string modifiedBy) : this()
    {
        this.CreatedBy = createdBy;
        this.ModifiedBy = modifiedBy;
    }
}

public class Department : Entity
{
    public string Name { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

public class Student : Entity
{
    public string Name { get; set; }
    public string Phone { get; set; }

    public string DepartmentId { get; set; }
    public virtual Department Department { get; set; }
}

答案 1 :(得分:1)

使用扩展方法架构

解决

修改后的来电方法

 public Student CreateModel()
    {
        Student model = new Student
        {
            Name = nameTextBox.Text,
            Phone = phoneTextBox.Text,
            DepartmentId = departmentComboBox.SelectedValue.ToString(),                
        };
        model.SetCommonValues();
        return model;
    }

SetCommonValues方法是

  public static Entity SetCommonValues(this Entity entity)
    {
        entity.Id = Guid.NewGuid().ToString();
        entity.Created = DateTime.Now;
        entity.Modified = DateTime.Now;
        entity.CreatedBy = Constants.UserName;
        entity.ModifiedBy = Constants.UserName;
        return entity;
    }

我可以将扩展方法标记为无效,但为了可扩展性,我返回了该对象。