-1
public EmployeeProcessOutput RetrieveEmployee(EmployeeProcessInput input)
{     
      var employee = input.Employee;
      if (input.EmployeeId == null)
          employee= CreateEmployee(employee);

      return new EmployeeProcessOutput
            {
                Employee = employee,
                State = "Stored" 
            };
}

private Employee CreateEmployee(employee)
{
      //Call a service to create an employee in the system using the employee 
      //info such as name, address etc and will generate an id and other 
      //employee info.
      var output = Service(employee)
      var employee = output.Employee;
      return employee;
 }

Is there a clean way and of passing the employee parameter to the CreateEmployee method. I feel that this line could be cleaner:

employee = CreateEmployee(employee);

Any thoughts?

Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
user1527762
  • 927
  • 4
  • 13
  • 29

2 Answers2

2

Are you talking about passing by reference instead of returning and assigning an Employee value? If so, you could do:

public EmployeeProcessOutput RetrieveEmployee(EmployeeProcessInput input)
{     
      var employee = input.Employee;
      if (input.EmployeeId == null)
          CreateEmployee(ref employee);

      return new EmployeeProcessOutput
            {
                Employee = employee,
                State = "Stored" 
            };
}

private void CreateEmployee(ref Employee employee)
{
      //Call a service to create an employee in the system using the employee 
      //info such as name, address etc and will generate an id and other 
      //employee info.
      var output = Service(employee)
      employee = output.Employee;
 }

Note that you must use the ref keyword because you are changing the value of the parameter itself. See this answer for more details.

That being said, this doesn't necessarily make the code "cleaner", and it is hard to tell exactly what you're asking. But for some reason I get the impression this is what you're referring to.

Community
  • 1
  • 1
schumacher574
  • 1,081
  • 1
  • 14
  • 32
0

I would not say there was anything wrong with what you are already doing, but if you absolutely want to change and simplify that line, maybe you can use an extension method?:

employee = employee.Create();

Which would call:

public static Employee Create(this employee)
{
    // ..call service, etc..

    return employee;
}

Note that this Create() method would need to reside in a separate public static class.

Kjartan
  • 18,591
  • 15
  • 71
  • 96