代码之家  ›  专栏  ›  技术社区  ›  arman

允许用户仅编辑/删除自己的内容(在通用存储库中)

  •  0
  • arman  · 技术社区  · 8 年前

    我想让用户只管理他们自己的内容(编辑/删除/物品列表…以及他们自己的内容所需的所有管理面板)。在我的项目中,我使用 工作单元 我有 基准位置 用于实现其余存储库(以避免重复代码);我知道我必须实现用户存储库,并在其他存储库中使用它来获取用户并继续操作。。。,但我想知道有没有办法 实施BaseRepository ? 还是另一种避免操作存储库和编写重复代码的方法?

    namespace ServiceLayer.Repository
    {
        public abstract class BaseRepository<T> : IBaseRepository<T> where T : BaseModel
        {
            private readonly IDbSet<T> _t;
            private readonly IUnitOfWork _unitOfWork;
            private IQueryable<T> _db;
            protected BaseRepository(IUnitOfWork unitOfWork)
            {
                _unitOfWork = unitOfWork;
                _t = _unitOfWork.Set<T>();
                _db = _t;
            }
            public virtual (IQueryable<T> List, int? PageId, int? PageCount) GetAll(int? pageId, Expression<Func<T, bool>>[] searchExpression, params Expression<Func<T, object>>[] includeExpressions)
            {
    
    
                if (includeExpressions != null && includeExpressions.Any())
                {
                    foreach (var expression in includeExpressions)
                    {
                        _db = _db.Include(expression);
                    }
                }
                if (searchExpression != null && searchExpression.Any())
                {
                    foreach (var expression in searchExpression)
                    {
                        _db = _db.Where(expression);
                    }
                }
    
                if (pageId == null) return (_db, null, null);
    
                if (pageId <= 0)
                    pageId = 1;
    
                int take = 5;
                int skip = ((int)pageId - 1) * take;
                int count = _db.Count();
                int pageCount = count / take;
    
    
    
                var list = _db.OrderByDescending(x => x.Id).Skip(skip).Take(take);
                return (list, (int)pageId, pageCount);
            }
    
    
            public virtual T Get(long id, params Expression<Func<T, object>>[] includeExpression)
            {
                if (includeExpression == null) return _t.Find(id);
    
                foreach (var expression in includeExpression)
                {
                    _db = _db.Include(expression);
                }
                return _db.SingleOrDefault(x => x.Id == id);
            }
    
            public virtual List<T> GetIdsArray(long?[] ids, params Expression<Func<T, object>>[] includeExpression)
            {
                if (ids == null) return null;
                if (includeExpression == null) return _t.Where(x => ids.Contains(x.Id)).ToList();
                foreach (var expression in includeExpression)
                {
                    _db = _db.Include(expression);
                }
                return _db.Where(x => ids.Contains(x.Id)).ToList();
            }
    
            public virtual T Update(T obj)
            {
                _unitOfWork.MarkAsChanged(obj);
                _unitOfWork.SaveAllChanges();
                return obj;
            }
    
            public virtual T Add(T obj)
            {
                _t.Add(obj);
                _unitOfWork.SaveAllChanges();
                return obj;
            }
    
            public virtual int Remove(T obj)
            {
                _t.Remove(obj);
                return _unitOfWork.SaveAllChanges();
            }
    
            public virtual int RemoveIdsArray(long?[] objs)
            {
                if (objs == null || objs.Length <= 0) return -5;
                var objects = GetIdsArray(objs, null);
                foreach (var o in objects)
                {
                    _t.Remove(o);
                }
                return _unitOfWork.SaveAllChanges();
            }
    
    
            public virtual (string[] fieldName, IEnumerable<string> DisplayAttribute) GetAllNameAndTitleOfDbTable()
            {
                var fieldName = typeof(T).GetProperties(BindingFlags.DeclaredOnly |
                                                        BindingFlags.Public |
                                                        BindingFlags.Instance)
                    .Select(property => property.Name)
                    .ToArray();
                var name = typeof(T).GetProperties(BindingFlags.DeclaredOnly |
                                                   BindingFlags.Public |
                                                   BindingFlags.Instance).Select(property =>
                    ((DisplayAttribute)property.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault())
                    ?.Name).ToArray();
                return (fieldName, name);
            }
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Tree Frog    8 年前

    我想我理解你想要实现的目标。

    我过去做过的事情是创建一个IOwnedEntity接口

    public interface IOwnedEntity
    {
        string EntityOwner { get; set;  }
    }
    

    然后由我的模型实现。

    public string EntityOwner
    {
        get { return Owner; }
        set { Owner = value; }
    }
    

    然后在您的存储库中,您可以检查T是否拥有并相应地进行过滤

    private bool EntityIsOwnedByOwner(T entity)
    {
        return (entity as IOwnedEntity).EntityOwner == CurrentLoggedOnUserID;
    }
    

    显然,这一切都需要一些工作才能在您的示例中发挥作用,但方法应该是合理的。