• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Asp.NetMVC+BootStrap+EF6.0实现简单的用户角色权限管理2

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

首先我们来写个类进行获取当前线程内唯一的DbContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    /// <summary>
    /// 当前线程内的数据上下文
    /// </summary>
    public class DbContextFactory {
        /// <summary>
        /// 获取当前线程内的数据上下文,如果当前线程内没有上下文,那么创建一个上下文,
        /// </summary>
        /// <returns>当前线程内的数据上下文</returns>
        public static DbContext GetCurrentDbContext() {
            DbContext currentContext = CallContext.GetData("CurrentDbContext") as DbContext;
            if (currentContext == null) {
                currentContext = new AuthorDesignContext();
                CallContext.SetData("CurrentDbContext", currentContext);
            }
            return currentContext;
        }
    }
}
View Code

CallContext 这个类是用来获取当前线程内唯一的数据,可以避免一次性创建出多个数据库上下文。

接下来是对基础的仓储进行编写(DAL):BaseRepository类

对数据的增删改查操作

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class BaseRepository<T> where T : class,new() {

        public DbContext db = DbContextFactory.GetCurrentDbContext();

        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T AddEntity(T entity) {
            db.Entry<T>(entity).State = EntityState.Added;
            db.SaveChanges();
            return entity;
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="property">需要修改的字段名称</param>
        /// <returns></returns>
        public bool EditEntity(T entity, string[] property) {
            DbEntityEntry<T> entry = db.Entry<T>(entity);
            entry.State = EntityState.Unchanged;
            foreach (var item in property) {
                entry.Property(item).IsModified = true;
            }
            return db.SaveChanges() > 0;
            //return true;
        }
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteEntity(T entity) {
            DbEntityEntry<T> entry = db.Entry<T>(entity);
            entry.State = EntityState.Deleted;
            return db.SaveChanges() > 0;
            // return true;
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> LoadEntities() {
            return db.Set<T>();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="whereLamda">查询条件</param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLamda) {
            return db.Set<T>().Where<T>(whereLamda);
        }
        /// <summary>
        /// 对查询结果进行升序排序
        /// </summary>
        /// <typeparam name="S">排序字段类型</typeparam>
        /// <param name="queryable">查询结果</param>
        /// <param name="orderLamda">排序表达式</param>
        /// <returns>根据排序条件排序好之后的排序结果</returns>
        public IOrderedQueryable<T> Order<S>(IQueryable<T> queryable, Expression<Func<T, S>> orderLamda) {
            return queryable.OrderBy(orderLamda);
        }
        /// <summary>
        /// 对排序结果再次进行升序排序
        /// </summary>
        /// <typeparam name="S">排序字段类型</typeparam>
        /// <param name="queryable">根据排序条件排序好之后的排序结果</param>
        /// <param name="orderLamda">排序表达式</param>
        /// <returns>根据排序条件排序好之后的排序结果</returns>
        public IOrderedQueryable<T> ThenOrder<S>(IOrderedQueryable<T> queryable, Expression<Func<T, S>> orderLamda) {
            return queryable.ThenBy(orderLamda);
        }
        /// <summary>
        /// 对查询结果进行降序排序
        /// </summary>
        /// <typeparam name="S">排序字段类型</typeparam>
        /// <param name="queryable">查询结果</param>
        /// <param name="orderLamda">排序表达式</param>
        /// <returns>根据排序条件排序好之后的排序结果</returns>
        public IOrderedQueryable<T> OrderDesc<S>(IQueryable<T> queryable, Expression<Func<T, S>> orderLamda) {
            return queryable.OrderByDescending(orderLamda);
        }
        /// <summary>
        /// 对排序结果再次进行降序排序
        /// </summary>
        /// <typeparam name="S">排序字段类型</typeparam>
        /// <param name="queryable">根据排序条件排序好之后的排序结果</param>
        /// <param name="orderLamda">排序表达式</param>
        /// <returns>根据排序条件排序好之后的排序结果</returns>
        public IOrderedQueryable<T> ThenOrderDesc<S>(IOrderedQueryable<T> queryable, Expression<Func<T, S>> orderLamda) {
            return queryable.ThenByDescending(orderLamda);
        }
        /// <summary>
        /// 对排序结果进行分页操作
        /// </summary>
        /// <param name="queryable">根据排序条件排序好之后的排序结果</param>
        /// <param name="nowNum">跳过序列中指定数量的元素</param>
        /// <param name="pageSize">从序列的开头返回指定数量的连续元素</param>
        /// <returns>指定长度的列表</returns>
        public IQueryable<T> LoadPageEnties(IOrderedQueryable<T> queryable, int nowNum, int pageSize) {
            return queryable.Skip<T>(nowNum + 1).Take<T>(pageSize);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="S">排序类型</typeparam>
        /// <param name="whereLamda">查询条件</param>
        /// <param name="orderLamda">排序条件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">页长</param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities<S>(Expression<Func<T, bool>> whereLamda, Expression<Func<T, S>> orderLamda, bool isDesc, int pageIndex, int pageSize, out int rowCount) {
            var temp = db.Set<T>().Where<T>(whereLamda);
            rowCount = temp.Count();
            if (isDesc)
                temp = temp.OrderByDescending<T, S>(orderLamda).Skip<T>(pageSize * (pageIndex - 1) + 1).Take<T>(pageSize);
            else
                temp = temp.OrderBy<T, S>(orderLamda).Skip<T>(pageSize * (pageIndex - 1) + 1).Take<T>(pageSize);
            return temp;
        }
    }
}
View Code

然后管理员类AdminRepository的DAL编写,继承BaseRepository类

using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class AdminRepository : BaseRepository<Admin>{
    }
}
View Code

我要做的是面向接口的(虽然没有BLL这层)那么接下来就对IDAL着层来进行编写,

首先也是IBaseRepository接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {

    public interface IBaseRepository<T> where T : class,new() {
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        T AddEntity(T entity);
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="property">需要修改的字段名称</param>
        /// <returns></returns>
        bool EditEntity(T entity, string[] property);
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool DeleteEntity(T entity);
        /// <summary>
        /// 查询
        /// </summary>
        IQueryable<T> LoadEntities();
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="whereLamda">查询条件</param>
        /// <returns></returns>
        IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLamda);
        /// <summary>
        /// 对查询结果进行升序排序
        /// </summary>
        /// <typeparam name="S">排序字段类型</typeparam>
        /// <param name="queryable">查询结果</param>
        /// <param name="orderLamda">排序表达式</param>
        /// <returns>根据排序条件排序好之后的排序结果</returns>
        IOrderedQueryable<T> Order<S>(IQueryable<T> queryable, Expression<Func<T, S>> orderLamda);
        /// <summary>
        /// 对排序结果再次进行升序排序
        /// </summary>
        /// <typeparam name="S">排序字段类型</typeparam>
        /// <param name="queryable">根据排序条件排序好之后的排序结果</param>
        /// <param name="orderLamda">排序表达式</param>
        /// <returns>根据排序条件排序好之后的排序结果</returns>
        IOrderedQueryable<T> ThenOrder<S>(IOrderedQueryable<T> queryable, Expression<Func<T, S>> orderLamda);
        /// <summary>
        /// 对查询结果进行降序排序
        /// </summary>
        /// <typeparam name="S">排序字段类型</typeparam>
        /// <param name="queryable">查询结果</param>
        /// <param name="orderLamda">排序表达式</param>
        /// <returns>根据排序条件排序好之后的排序结果</returns>
        IOrderedQueryable<T> OrderDesc<S>(IQueryable<T> queryable, Expression<Func<T, S>> orderLamda);
        /// <summary>
        /// 对排序结果再次进行降序排序
        /// </summary>
        /// <typeparam name="S">排序字段类型</typeparam>
        /// <param name="queryable">根据排序条件排序好之后的排序结果</param>
        /// <param name="orderLamda">排序表达式</param>
        /// <returns>根据排序条件排序好之后的排序结果</returns>
        IOrderedQueryable<T> ThenOrderDesc<S>(IOrderedQueryable<T> queryable, Expression<Func<T, S>> orderLamda);
        /// <summary>
        /// 对排序结果进行分页操作
        /// </summary>
        /// <param name="queryable">根据排序条件排序好之后的排序结果</param>
        /// <param name="nowNum">跳过序列中指定数量的元素</param>
        /// <param name="pageSize">从序列的开头返回指定数量的连续元素</param>
        /// <returns>指定长度的列表</returns>
        IQueryable<T> LoadPageEnties(IOrderedQueryable<T> queryable, int nowNum, int pageSize);
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="S">排序类型</typeparam>
        /// <param name="whereLamda">查询条件</param>
        /// <param name="orderLamda">排序条件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">页长</param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        IQueryable<T> LoadEntities<S>(Expression<Func<T, bool>> whereLamda, Expression<Func<T, S>> orderLamda, bool isDesc, int pageIndex, int pageSize, out int rowCount);
    }
}
View Code

然后管理员接口IAdminRepository 继承IBaseRepository

using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IAdminRepository:IBaseRepository<Admin> {
    }
}
View Code

接下来我们更改下原来的 管理员类AdminRepository的DAL编写,引用了IAdminRepository 的接口

using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class AdminRepository : BaseRepository<Admin>, IAdminRepository {
    }
}
View Code

然后对其他Model也进行相同的编写。下面附上代码:

首先是IDAL,接口这里。

using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IActionToPageRepository : IBaseRepository<ActionToPage> {
    }
}
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IAdminLoginLogRepository : IBaseRepository<AdminLoginLog> {
    }
}
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IAdminOperationRepository : IBaseRepository<AdminOperation> {
    }
}
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IAdminToPageRepository : IBaseRepository<AdminToPage> {
    }
}
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IAuthoryRepository : IBaseRepository<Authory> {
    }
}
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IAuthoryToPageRepository : IBaseRepository<AuthoryToPage> {
    }
}
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IPageActionRepository : IBaseRepository<PageAction> {
    }
}
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.IDAL {
    public interface IPageMenuRepository : IBaseRepository<PageMenu> {
    }
}
View Code

其次是DAL。

using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class ActionToPageRepository : BaseRepository<ActionToPage> ,IActionToPageRepository{
    }
}
using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class AdminLoginLogRepository : BaseRepository<AdminLoginLog>, IAdminLoginLogRepository {
    }
}
using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class AdminOperationRepository : BaseRepository<AdminOperation>, IAdminOperationRepository {
    }
}
using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class AdminToPageRepository : BaseRepository<AdminToPage>, IAdminToPageRepository {
    }
}
using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class AuthoryRepository : BaseRepository<Authory>, IAuthoryRepository {
    }
}
using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class AuthoryToPageRepository : BaseRepository<AuthoryToPage>, IAuthoryToPageRepository {
    }
}
using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class PageActionRepository : BaseRepository<PageAction>, IPageActionRepository {
    }
}
using AuthorDesign.IDAL;
using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class PageMenuRepository : BaseRepository<PageMenu>,IPageMenuRepos 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net 上传大文件解决方案发布时间:2022-07-10
下一篇:
ASP.NET 实践:自定义 SiteMapPath 控件的外观发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap