I want to display on a view the EmployeeHistory
view model sorted by elements.assignEffDateNext
and I want to instantiate and populate the view model from the controller. The code analysis is saying that the IList<IDMSElementDate> EmployeePositionsOverTime
should be read only. I kind of understand why.
This is what I started with.
public class EmployeeHistory
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public IList<IDMSElementData> EmployeePositionsOverTime { get; set; }
}
public async Task<ActionResult> EmployeeHistory(int? employeeID)
{...//this part retrieves the elements from an api...
Employee emp = db.Employees.Find(employeeID);
string employeeName = emp.EmployeeFullName;
var sortedList = elements.Elements.OrderByDescending(e => e.assignEffDateNext).ToList();
EmployeeHistory empHist = new EmployeeHistory
{
EmployeeID = (int)employeeID,
EmployeeName = employeeName,
EmployeePositionsOverTime = sortedList,
};
return View("EmployeeHistory", empHist);
}
This is what I have changed it to. I added a constructor and changed the properties to be read-only so the type is immutable now and the analyzer is no longer giving me a warning. I was wondering whether this is better now and whether there is anything else about it that I should change?
public class EmployeeHistory
{
public EmployeeHistory(int employeeID, string employeeName, IList<IDMSElementData> employeePositionsOverTime)
{
EmployeeID = employeeID;
EmployeeName = employeeName;
EmployeePositionsOverTime = employeePositionsOverTime;
}
public int EmployeeID { get; }
public string EmployeeName { get; }
public IList<IDMSElementData> EmployeePositionsOverTime { get; }
}
public async Task<ActionResult> EmployeeHistory(int? employeeID)
{...//this part retrieves the elements from an api...
Employee emp = db.Employees.Find(employeeID);
string employeeName = emp.EmployeeFullName;
var sortedList = elements.Elements.OrderByDescending(e => e.assignEffDateNext).ToList();
EmployeeHistory empHist = new EmployeeHistory((int)employeeID,employeeName,sortedList);
return View("EmployeeHistory", empHist);
}
async
but I don't see youawait
ing anything there. \$\endgroup\$