延迟加载类
public class LazyLoader<TModel> where TModel:class
{
TModel _data;
Func<TModel> _loadDataFunc = null;
bool _loaded = false;
public LazyLoader():this(null){ }
public LazyLoader(Func<TModel> loadDataFunc)
{
_loadDataFunc = loadDataFunc;
_data = null;
_loaded = false;
}
public TModel QueryData()
{
if (!_loaded && _loadDataFunc != null)
{
_data = _loadDataFunc();
}
return _data;
}
public void SetData(TModel value)
{
_data = value;
_loaded = true;
}
}
LazyLoader
使用方法(简单举列)
比如员工菜单 可能你再使用这个员工对象的时候并不需要去加载他的菜单 当你要用的时候再加载 这时候就需要延迟加载了
public class Employee
{
public LazyLoader<IEnumerable<Menu>> _menus;
public Employee(Guid employeeId, string employeeNo, string name, string password, string email, string phone, bool enabled, bool isAdministrtor, DateTime registerDate, string remark, string ipLimitation)
{
EmployeeId = employeeId;
EmployeeNo = employeeNo;
Name = name;
Password = password;
Email = email;
Phone = phone;
Enabled = enabled;
IsAdministrator = isAdministrtor;
RegisterDate = registerDate;
Remark = remark;
IpLimitation = ipLimitation;
_menus = new LazyLoader<IEnumerable<Menu>>(() => SystemResourceService.QueryMenusByEmployeeId(employeeId));
}
public Guid EmployeeId { get; private set; }
public string EmployeeNo { get; private set; }
public string Name { get; private set; }
public string Password { get; private set; }
public string Email { get; private set; }
public string Phone { get; private set; }
public bool Enabled { get; set; }
public bool IsAdministrator { get; private set; }
public DateTime RegisterDate { get; private set; }
public string Remark { get; private set; }
public string IpLimitation { get; set; }
public object Id
{
get { return EmployeeId; }
}
}
Employee

浙公网安备 33010602011771号