使用Category属性c#在DTO中共享属性

时间:2016-12-11 12:39:36

标签: c# model dto system.componentmodel

我正在使用员工模型,它包含我在How to use the DTO efficiently 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
}

我怎么能实现这个目标?在这方面你能不能请任何人帮忙。

1 个答案:

答案 0 :(得分: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; }
}