0

I'm working on Employee Model, it contains all the information about the Employee already I posted the same in How to use the DTO efficiently based on Scenario in C#. How could I share the single property for the multiple groups using Category attribute c#.

For Example:

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

I'm having the following four methods for fetching records of Employee

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
}

How could I achieve this? could you please any one help in this regards.

Community
  • 1
  • 1
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130

1 Answers1

0

A sample, this is the common use for 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
    };
}

Or

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; }
}
ArDumez
  • 931
  • 6
  • 24
  • I asked the question How to share the property using `Category attribute - c#`. But in the answer section shows a new DTO for Name section. - It's not suit for my question. – B.Balamanigandan Dec 11 '16 at 13:32
  • I have edit my answer, Have you an sample how you want use Category attribute ? – ArDumez Dec 11 '16 at 13:44
  • Kindly refer the https://msdn.microsoft.com/en-us/library/system.componentmodel.categoryattribute_properties(v=vs.110).aspx for more information about `Category attribute` – B.Balamanigandan Dec 11 '16 at 13:47