In a business class I have :
class Employee{
public Employee() {
m_Manager = new Lazy<Manager>( () => return myRepository.GetManager(ManagerId); );
}
public int ManagerId { get; set;}
private Lazy<Manager> m_Manager;
public Manager Manager {
get {
return m_Manager.Value;
}
}
}
This is working correctly, the custom repository is called only if accessing the Manager property.
Now I want to "reset" my manager property if the ManagerId changed. How to do that ?
I can do :
class Employee{
public Employee() {
m_Manager = new Lazy<Manager>( () => return myRepository.GetManager(ManagerId); );
}
private int m_ManagerId;
public int ManagerId {
get { return m_ManagerId;}
set {
if(m_ManagerId != value)
{
m_ManagerId = value;
m_Manager = new Lazy<Manager>( () => return myRepository.GetManager(ManagerId); );
}
}
}
private Lazy<Manager> m_Manager;
public Manager Manager {
get {
return m_Manager.Value;
}
}
}
Is there a cleaner way to do that ? Isn't there a m_Manager.Reset()
or something like this ?
question from:
https://stackoverflow.com/questions/5961252/reset-system-lazy 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…