埋藏在心里的种子
一直以来都想做一件大事.这个计划在我心里酝酿了快两年了.迟迟没有行动.终于在某种冲动下,我决定了要造轮子
.我觉得如果是重复的轮子,那就没必要去费这个心思,如果你觉得现有的轮子不好,那你可以去改造她,让她变得更完美.没必要白手起家
.这才是开源的优势和目的,并不是大家重复去造相同的轮子.(仅代表个人观点.不喜勿喷!!)
背景
言归正传,讲述一下之前的项目,因为分层和命名比较规范.每个类的职责也相对比较清晰.因为项目是从无到有的,前期的人力也比较有限,所以为了快速开发,我们自己写了个代码生成器.从建表到基础代码生成一键搞定的模式.期间我们的规范和代码生成器不断的适配.
结构和规范如下:
- 实体层
命名规范是以文件夹名称为后缀.具体含义 我想不用再解释了.
- 服务层
就是抽象接口层 和 实现层.
这里我大概做一个示例
/// <summary>
/// 学生服务
/// </summary>
public class StudentService : IStudentService
{
private readonly IRepository _repository;
public StudentService(IRepository repository)
{
_repository = repository;
}
/// <summary>
/// 新增
/// </summary>
/// <returns></returns>
public async Task<int> AddAsync(StudentAddInput input)
{
return await _repository.InsertAsync<StudentEntity>(input.MapTo<StudentEntity>());
}
/// <summary>
/// 修改
/// </summary>
/// <returns></returns>
public async Task<int> UpdateAsync(StudentModifyInput input)
{
return await _repository.UpdateAsync<StudentEntity>(input.MapTo<StudentEntity>());
}
/// <summary>
/// [单表]获取单个
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<StudentEntity> GetEntityAsync(StudentQueryInput input)
{
var condition = this.GetEntityExpression(input);
return await _repository.FindEntityAsync<StudentEntity>(condition);
}
/// <summary>
/// [单表]获取集合
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<IEnumerable<StudentEntity>> GetEntityListAsync(StudentQueryInput input)
{
var condition = this.GetEntityExpression(input);
return await _repository.FindListAsync<StudentEntity>(condition);
}
/// <summary>
/// [单表]获取分页
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PageEntity<IEnumerable<StudentEntity>>> GetPageAsync(PageEntity<StudentQueryInput> input)
{
var condition = this.GetEntityExpression(input.Data);
//分页查询
var (data, total) = await _repository.FindListAsync(condition, input.OrderField, input.Ascending, input.PageSize, input.PageIndex);
return new PageEntity<IEnumerable<StudentEntity>>
{
PageIndex = input.PageIndex,
Ascending = input.Ascending,
PageSize = input.PageSize,
OrderField = input.OrderField,
Total = total,
Data = data
};
}
/// <summary>
/// 获取SqlSugar的表达式目录树
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private Expression<Func<StudentEntity, bool>> GetEntityExpression(StudentQueryInput input)
{
var condition = LinqExtensions.True<StudentEntity>();
//主键Id
if (!input.Id.IsNullOrEmpty())
condition = condition.And(x => x.Id == input.Id);
//年龄
if (!input.Age.IsNull())
condition = condition.And(x => x.Age == input.Age);
//名称
if (!input.Name.IsNullOrEmpty())
condition = condition.And(x => x.Name == input.Name);
//描述
if (!input.Remark.IsNullOrEmpty())
condition = condition.And(x => x.Remark == input.Remark);
//是否有效
if (input.Enabled != -1)
condition = condition.And(x => x.Enabled == input.Enabled);
return condition;
}
}
可是没用多久就感觉到不是我想要的样子.我隐约闻到代码"坏掉"的味道.
思考
于是我开始思考,这些重复的代码,虽然生成很快,用起来也很舒服.但是他不是我想要的样子.重复的代码
越多,以后改起来也是巨大的麻烦.
- 比如说以后加一个数据库字段,或者加一查询字段.还要去改.还存在改错的风险(虽然风险小,但是还是存在的)
- 还有新增,修改之类的.都是基本的.其实抽象都可以解决的.
我想查询的关键就在条件的拼装上.如果有了拼装,基础的查询照样可以封装到抽象实现类里.于是乎,这种想法就萌芽了.一直以来越长越大... 但是迟迟还是没有动手.
现在
由于到了一个新环境,这里是把查询写到了仓储.但是本质还是没有变.还是每个实体写一遍.由于没了代码生成器,还要手写..我忍无可忍..做下决定,我要造这个轮子.要让开发者解放双手
.
以上就是我造轮子背景原因.
以下就是我的造的轮子:
BaseQuery