0

我正在研究员工模型,它包含有关员工的所有信息,我已经在How to use the DTO based on Scenario in C# 中发布了相同的信息。如何使用 Category 属性 c# 共享多个组的单个属性。

例如:

public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public string HomePhone { get; set; }
    public string MobilePhone { get; set; }
}

我有以下四种获取员工记录的方法

public Employee GetEmployeeName(int id)
{
    // The return should contain only FirstName, MiddleName and LastName. 
    // The rest of the properties should be non-accessable (i.e., Private)
}

public Employee GetEmployeeContacts(int id)
{
    // The return should contain only EmailAddress, HomePhone and MobilePhone. 
    // The rest of the properties should be non-accessable (i.e., Private)
}

public Employee GetEmployeeNameEmail(int id)
{
    // The return should contain only FirstName, MiddleName, LastName and EmailAddress. 
    // The rest of the properties should be non-accessable (i.e., Private)
}

public Employee GetEmployee(int id)
{
    // It should return the entire Employee object
}

我怎么能做到这一点?在这方面你能请任何人帮忙吗?

4

1 回答 1

0

一个示例,这是 DTO 的常见用途:

public class EmployeeNameDto
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
}

public EmployeeNameDto GetEmployeeName(int id)
{
    Employee emplpoyee = employeeRepository.Find(id):
    return new EmployeeNameDto() {
      EmployeeId = emplpoyee.EmployeeId,
      FirstName = emplpoyee.FirstName,
      MiddleName = emplpoyee.MiddleName,
      LastName = emplpoyee.LastName
    };
}

或者

public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; 
      set {
        if (condition == false)
          throw new Exception(" is Read Only !")
      }
    }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public string HomePhone { get; set; }
    public string MobilePhone { get; set; }
}
于 2016-12-11T12:59:13.620 回答