我有一个静态方法,当在多线程环境中调用该方法时,该请求似乎已被其他线程修改。例如,请参见下面的代码。
在我的情况下,如果我将EmployeeService.UpdateEmployees设为非静态,则它会给我Im所期望的结果,但如果它是静态的,则它将以另一种方式出现。
有人能启发我为什么会这样吗?什么时候将方法设为静态?
public class Test
{
public void DoSomething(EntityRequest entityRequest)
{
EmployeeService.UpdateEmployees(entityRequest.Employees);
}
}
public class EmployeeService
{
public static void UpdateEmployees(List<Employee> employees)
{
EmployeeDBService.Update(employees);
}
public static IEnumerable<Employee> GetEmployees(EmployeeFind request)
{
//some logic here
}
}
public class EmployeeDBService
{
public static void Update(List<Employee>employees)
{
//some logic here build SP request
}
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime BirthDay { get; set; }
public Guid RowGuid { get; set; }
}
public class EmployeeFind
{
public EmployeeFind()
{
Employees = new List<Employee>();
}
public EmployeeSearch SearchType { get; set; }
public IList<Employee> Employees { get; set; }
}
public enum EmployeeSearch
{
ByDepartmentId
}