繁体   English   中英

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

[英]C# pass child object to a method to be filled with the common properties of base class

我有一个基本实体类,它由两个类派生,名为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;            
    }

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

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

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; }
}

使用扩展方法体系结构解决

修改了调用方法

 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;
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM