zoukankan      html  css  js  c++  java
  • 自定义延迟加载类

    延迟加载类

    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
  • 相关阅读:
    Org4的約會的Form_Load
    MSCRM 4 Remove 'Add Existing xxxxx to this record' button
    出售剩余时间
    MSCRM中disabled和Disabled屬性的區別
    一些常用的sql
    約會的客戶變更時取其它表的資料
    MSCRM儲存disabled欄位的值
    CRM显示产品图片
    顯示Object的所有屬性及處理ContactId只列出ParentCustomer的資料的javascript
    浅谈递归过程以及递归的优化
  • 原文地址:https://www.cnblogs.com/wuxl360/p/5691164.html
Copyright © 2011-2022 走看看