书接上文,继续搭建我们基于.netCore 的开发框架。首先是我们的项目分层结构。

.netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使用

 

这个分层结构,是参考张老师的分层结构,但是实际项目中,我没有去实现仓储模型。因为我使用的是EFCore ,最近也一直在想,EFCore 在我们的架构体系中到底扮演着什么样的角色??

当然,实现仓储层,也有他的好处,如果真的以后要更换ORM框架的时候,不用去更改服务层逻辑,不用直接在仓储里面做更改就可以了。但是对于小项目,可能运行个十年都不会换数据库,不会换ORM的项目,仓储层的意义在哪?

希望对此有自己想法的朋友一起讨论。在本系列里,我将保留仓储层。

上面的分层结构,很容易就搭建好了,但是基于.NetCore 的项目,可能最主要的一点就是如何利用好DI,怎么来实现依赖注入。

.netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使用

依照上图中的依赖关系,我们以此构建自己相应层级中的内容。

.netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使用

分别在IRepository和Repository项目下,新建Base文件夹,并分别建立IBaseRepository和BaseRepository

具体代码如下:

IBaseRepository.cs:

 1 using Sincere.Core.Model;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Data;
 5 using System.Data.SqlClient;
 6 using System.Linq.Expressions;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 
10 namespace Sincere.Core.IRepository.Base
11 {
12     public interface IBaseRepository<TEntity> where TEntity : class
13     {
14         Task<int> Execute(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text);
15         Task<List<TEntity>> Query(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text);
16         Task<bool> Insert(TEntity model);
17         Task<bool> InsertRange(List<TEntity> datas);
18 
19         Task<int> Del(TEntity model);
20 
21         Task<int> DelBy(Expression<Func<TEntity, bool>> delWhere);
22 
23         Task<int> Modify(TEntity model);
24 
25         Task<int> Modify(TEntity model, params string[] propertyNames);
26 
27         Task<int> ModifyBy(TEntity model, Expression<Func<TEntity, bool>> whereLambda, params string[] modifiedPropertyNames);
28 
29         Task<List<TEntity>> GetList();
30 
31         Task<List<TEntity>> GetListBy(Expression<Func<TEntity, bool>> whereLambda);
32 
33         Task<TEntity> GetModelById(Expression<Func<TEntity, bool>> whereLambda);
34 
35         Task<List<TEntity>> GetListBy<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true);
36 
37         Task<List<TEntity>> GetListBy<TKey>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true);
38 
39         Task<List<TEntity>> GetListBy<TKey1, TKey2>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true);
40 
41         Task<List<TEntity>> GetListBy<TKey1, TKey2>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true);
42 
43         Task<List<TEntity>> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true);
44 
45         Task<PageModel<TEntity>> GetPagedList<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true, int pageIndex = 1, int pageSize = 20);
46 
47         void RollBackChanges();
48 
49     }
50 }
View Code

相关文章: