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

时间:2017-11-15 11:02:17

标签: c# asp.net asp.net-mvc asp.net-identity

我想让用户只管理他们自己的内容(编辑/删除/列出的东西......以及管理面板需要管理面板的所有内容)。在我的项目中,我使用 unitOfwork GenericRepositories ,我有 BaseRepositoy 来实现其余的存储库(以避免重复的代码);我知道我必须实现用户存储库并在其他存储库中使用它来获取用户并继续操作...,但我想知道是否有任何方法实现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 个答案:

答案 0 :(得分:1)

我想我明白你想要实现的目标。

我过去做过的事情就是创建一个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;
}

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