.net core仓储(Repository)模型

仓储是存在于工作单位和数据库之间单独分离出来的,是对数据访问层的封装。其优点是:达到了分离关注点;提高了对数据库访问的维护,对仓储的改变并不会改变业务逻辑。使用仓储模型,可以实现解耦数据访问层与业务层。

仓储层的搭建:

一、定义Repository接口类(IBaseRepository):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public interface IBaseRepository<T> where T : class
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t"></param>
        void AddEntity(T t);
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        void DeleteEntity(T t);
 
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        void UpdateEntity(T t);
 
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        IQueryable<T> GetAll();
    }

 二、定义Repository实体类(BaseRepository),继承Repository接口类(IBaseRepository),实现其接口中的方法:

 

复制代码
public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        /// <summary>
        /// DB上下文
        /// </summary>
        private readonly RBACDBContext _context;

        public BaseRepository(RBACDBContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void AddEntity(T t)
        {
            throw new NotImplementedException();
        }/// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void DeleteEntity(T t)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> GetAll()
        {
            throw new NotImplementedException();
        }/// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdateEntity(T t)
        {
            throw new NotImplementedException();
        }
    }
复制代码

三、进行简单的数据库操作:

1、定义用户的服务接口(IUserService):

复制代码

/// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

public
interface IUserService : IBaseService<UserInfo> { UserInfo UserLogin(string? userName, string? password); }
复制代码

2、定义用户的服务类(UserService),实现用户服务接口(IUserService)中的方法:

复制代码
public class UserService : BaseService<UserInfo> , IUserService
    {
        private readonly IUserRepository _userRepository;public UserService(IUserRepository userRepository) : base(userRepository)
        {
            _userRepository = userRepository;
        }/// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public UserInfo UserLogin(string? userName, string? password)
        {
            var userInfo = _userRepository.GetAll().FirstOrDefault(x => x.UserName == userName && x.Password == password);
            return userInfo;
        }
    }
}
复制代码

 

posted @   $日落青山  阅读(439)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示