这个协会ORM怎么样?
问题描述:
public class Employee
{
public byte ID { get; set; }
public string Name { get; set; }
}
public class Task
{
public int ID { get; set; }
public string Description { get; set; }
private List<Employee> employeeTask ;
public List<Employee> EmployeeTask
{
get
{
if (employeeTask == null)
{
employeeTask = //Any Source changeable ;
}
return employeeTask;
}
}
}
class Program
{
void main()
{
List<Task> ListTask = //fill with groub of Tasks;
/*When Call
property EmployeeTask in every Object in list (ListTask),
the property(EmployeeTask) in all Object
have the Same content (have last content element)*/
}
}
如何在ListTask中创建每个对象拥有自己的内容(属性EmployeeTask)??
How i can make every object in ListTask have Content of its own( property EmployeeTask )??
答
只需完成类任务
。将 getter 添加到属性EmployeeTask
或添加一个构造函数,接受类型List< Employee> 的值
初始化此属性的值。
但是,即使现在代码不完整,整个设计也值得怀疑。您需要将属性封装在任务列表中。永远不要离开字段employeeTask
未初始化,在声明点用空列表初始化它。 (可选)为列表提供列表界面,而不暴露列表本身。-SA
Just complete the classTask
. Either add the getter to the propertyEmployeeTask
or add a constructor accepting the value of the typeList<Employee>
to initialize the value of this property.
However, the whole design is questionable even now when the code is incomplete. You need to property encapsulate the list of tasks. Never leave the fieldemployeeTask
uninitialized, initialize it at the point of declaration with an empty list. Optionally, provide a list interface for the list without exposing the list itself.—SA