/// <summary>
        /// 根据Key获取实体
        /// </summary>
        /// <param name="keyValue">实体键</param>
        /// <returns>返回查询结果</returns>
        /// <exception cref="AccessException">不允许读取时抛出此异常</exception>
        public virtual TEntity GetByKey(object keyValue)
        {
            if (CheckAccess(OnCanRead))
            {
                TEntity entity = null;
                Invoke(() => entity = _context.FindById <TEntity>(keyValue.ToString()));
                return(entity);
            }

            throw new AccessException("CanRead");
        }
Example #2
0
        /// <summary>Finds a user</summary>
        /// <param name="userId">用户编号</param>
        /// <returns>返回查询到的用户实体</returns>
        public Task <TUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(userId))
            {
                throw new ArgumentException("Null or empty argument: userId");
            }

            return(TaskHelper.RunTask(() =>
            {
                var result = _objectStorage.FindById <TUser>(userId);


                return result;
            }));
        }
        /// <summary>
        /// 根据一组key获取实体
        /// </summary>
        /// <param name="keyValues">一组查询条件</param>
        /// <returns>返回查询到的实例</returns>
        /// <exception cref="System.InvalidOperationException">查询时可能会抛出异常</exception>
        /// <exception cref="AccessException">不允许读取时抛出此异常</exception>
        public virtual TEntity GetByKeys(params object[] keyValues)
        {
            if (CheckAccess(OnCanRead))
            {
                TEntity entity = null;
                Invoke(() =>
                {
                    foreach (object t in keyValues)
                    {
                        entity = Context.FindById <TEntity>(t.ToString());
                        if (entity != null)
                        {
                            break;
                        }
                    }
                });
                return(entity);
            }

            throw new AccessException("CanRead");
        }
Example #4
0
 /// <summary>Find a role by id</summary>
 /// <param name="roleId">角色编号</param>
 /// <returns>返回角色</returns>
 public Task <TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
 {
     return(TaskHelper.RunTask(() => _objectStorage.FindById <TRole>(roleId)));
 }