• 自定义延迟加载类


    延迟加载类

    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
  • 相关阅读:
    LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、
    LINQ标准查询操作符(一)——select、SelectMany、Where、OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse
    LINQ GroupBy
    C# EF使用SqlQuery直接操作SQL查询语句或者存储过程
    mycat工作原理
    Linux系统启动过程详解
    jump堡垒机配置使用
    jumpserver 堡垒机环境搭建(图文详解)
    pip安装
    判断网站响应时间 脚本
  • 原文地址:https://www.cnblogs.com/wuxl360/p/5691164.html
Copyright © 2020-2023  润新知