当前位置:网站首页>An excellent orm in dotnet circle -- FreeSQL
An excellent orm in dotnet circle -- FreeSQL
2022-07-03 00:29:00 【InfoQ】
Preface
Recommended reasons
The application case
Injection service
public void ConfigureServices(IServiceCollection services)
{
// Log initialization
services.ConfigureLog();
// orm frame
services.ConfigureOrm(Configuration);
// Cache configuration
services.ConfigureRedis(Configuration);
// Login authorization , Cross domain policy ,session To configure
services.ConfigurePolicy(Configuration);
//mvc To configure
services.ConfigureMvc(Configuration);
// Other plug-in configuration
services.ConfigurePlug(Configuration);
}
static IdleBus<IFreeSql> ib = new IdleBus<IFreeSql>(TimeSpan.FromMinutes(10));
public static void ConfigureOrm(this IServiceCollection services, IConfiguration configuration)
{
#region orm frame
ib.Register("db_system", () => new FreeSqlBuilder().UseConnectionString(DataType.SqlServer, configuration.GetConnectionString("CysccSystemConnString")).Build());
ib.Register("db_castic", () => new FreeSqlBuilder().UseConnectionString(DataType.SqlServer, configuration.GetConnectionString("CysccCasticConnString")).Build());
ib.Register("db_robot", () => new FreeSqlBuilder().UseConnectionString(DataType.SqlServer, configuration.GetConnectionString("CysccRobotConnString")).Build());
ib.Register("db_passport", () => new FreeSqlBuilder().UseConnectionString(DataType.SqlServer, configuration.GetConnectionString("PassportConnString")).Build());
ib.Register("db_matchai", () => new FreeSqlBuilder().UseConnectionString(DataType.SqlServer, configuration.GetConnectionString("MatchAIConnString")).Build());
ib.Register("db_checkin", () => new FreeSqlBuilder().UseConnectionString(DataType.SqlServer, configuration.GetConnectionString("CheckInConnString")).Build());
services.AddSingleton(ib);
#endregion
}
Generate model objects

Write basic operation methods and interfaces
public class MatchAiRespository<T>: IMatchAiRespository<T> where T : class
{
public class MatchAiRespository<T>: IMatchAiRespository<T> where T : class
{
IdleBus<IFreeSql> fsql = null;
const string conn_str = "db_matchai";
public MatchAiRespository(IdleBus<IFreeSql> fsql)
{
this.fsql = fsql;
}
/// <summary>
/// Page search , And return the total
/// </summary>
/// <param name="predicate"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="total"></param>
/// <returns></returns>
public List<T> getList(Expression<Func<T, bool>> predicate, int pageIndex, int pageSize, out long total)
{
return fsql.Get(conn_str).Select<T>().Where(predicate).Count(out total).Page(pageIndex, pageSize).ToList();
}
/// <summary>
/// Get the number of records
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public async Task<long> getCountAsync(Expression<Func<T, bool>> predicate)
{
return await fsql.Get(conn_str).Select<T>().Where(predicate).CountAsync();
}
/// <summary>
/// Add a single entry , Return the number of affected items
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public int addItem(T t)
{
return fsql.Get(conn_str).Insert<T>(t).ExecuteAffrows();
}
...
}
}
public interface IMatchAiRespository<T> where T:class
{
/// <summary>
/// Page search , And return the total
/// </summary>
/// <param name="predicate"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="total"></param>
/// <returns></returns>
List<T> getList(Expression<Func<T, bool>> predicate, int pageIndex, int pageSize, out long total);
/// <summary>
/// Get the number of records ( asynchronous )
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
Task<long> getCountAsync(Expression<Func<T, bool>> predicate);
/// <summary>
/// Add a single entry , Return the number of affected items
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
int addItem(T t);
...
}
public class CertRecordRepo : MatchAiRespository<Cert_Template>, Interfaces.ICertTemplateRespo
{
private IdleBus<IFreeSql> fsql;
const string conn_str = "db_matchai";
public CertRecordRepo(IdleBus<IFreeSql> fsql) : base(fsql)
{
this.fsql = fsql;
}
/// <summary>
/// Obtain certificate issuance records
/// </summary>
/// <param name="whereJsonStr"></param>
/// <param name="total"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public dynamic GetCertRecords(string whereJsonStr, out long total, int pageIndex = 1, int pageSize = 10)
{
DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(whereJsonStr);
return fsql.Get(conn_str).Select<Cert_Record>()
.WhereDynamicFilter(dyfilter)
.Count(out total)
.Page(pageIndex, pageSize)
.ToList();
}
/// <summary>
/// Get certificate export record ( Trials )
/// </summary>
/// <param name="whereJsonStr"></param>
/// <returns></returns>
public async Task<List<CostumModels.CertRecordModel>> GetCertRecordsForExcel(string whereJsonStr)
{
DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(whereJsonStr);
var list = await fsql.Get(conn_str).Select<Cert_Record, Cert_Template, VIW_ScratchProgramApplyMain>()
.LeftJoin((a, b, c) => a.Template_id == b.TemplateID)
.LeftJoin((a, b, c) => a.ProjectNo == c.ProjectNo)
.WhereDynamicFilter(dyfilter)
.ToListAsync((a, b, c) => new CostumModels.CertRecordModel
{
CertContent = a.CertContent,
ProgramTypeCaption = c.ProgramTypeCaption,
ProjectNo = a.ProjectNo,
ProgramTypeCaptionParent = c.ProgramTypeCaptionParent,
ProjectSortCaption = c.ProjectSortCaption,
ProjectWorkCaption = c.ProjectWorkCaption,
TeamCaption = c.TeamCaption,
TemplateTypeCaption = b.TemplateTypeCaption,
ProjectTitle = c.ProjectTitle,
Score = c.Score,
//HasScore = c.HasScored,
AwardName = a.AwardName,
ProvinceShortName = c.ProvinceShortName,
CertType = a.CertType,
CertNo = a.CertNum
});
return list;
}
}
controller
[Area("space")]
[Authorize]
public class CertController : Controller
{
private readonly ICertRecordRepo certRepo;
private readonly INpoiExcelOperationService npoi;
private readonly IWebHostEnvironment en;
private readonly IResponseHelper resp;
/// <summary>
/// Constructors
/// </summary>
/// <param name="certRepo"></param>
/// <param name="resp"></param>
/// <param name="en"></param>
/// <param name="npoi"></param>
public CertController( ICertRecordRepo certRepo,IResponseHelper resp,IWebHostEnvironment en, INpoiExcelOperationService npoi)
{
this.certRepo = certRepo;
this.en = en;
this.resp = resp;
this.npoi = npoi;
}
/// <summary>
/// Get the issuance record
/// </summary>
/// <param name="whereJsonstr"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
[ResponseCache(Duration =100,VaryByQueryKeys = new string[] {"whereJsonstr","rid"})]
public IActionResult GetCertRecords(string whereJsonstr, int pageIndex = 1, int pageSize = 10)
{
long total = 0;
return Json(_resp.success(new { items = _recordRepo.GetCertRecords(whereJsonstr, out total, pageIndex, pageSize), total }));
}
/// <summary>
/// Export table
/// </summary>
/// <param name="npoi"></param>
/// <param name="whereJsonstr"></param>
/// <returns></returns>
[HttpPost,ValidateAntiForgeryToken]
public async Task<IActionResult> ExportCertRecords([FromServices] INpoiExcelOperationService npoi, string whereJsonstr)
{
var list = await _recordRepo.GetCertRecordsForExcel(whereJsonstr);
return Json(await npoi.ExcelDataExportTemplate($" Event 【{list.First().ProgramTypeCaption}】 Certificate issuance record ", list.First().ProgramTypeCaption, list, _en.WebRootPath));
}
}



边栏推荐
- NC24840 [USACO 2009 Mar S]Look Up
- Briefly talk about other uses of operation and maintenance monitoring
- How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
- 洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
- Automated defect analysis in electron microscopic images-论文阅读笔记
- [IELTS reading] Wang Xiwei reading P1 (reading judgment question)
- Introduction of UART, RS232, RS485, I2C and SPI
- 微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
- [reading notes] phased summary of writing reading notes
- Go custom sort
猜你喜欢
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
MySQL advanced learning notes (III)
Which software can translate an English paper in its entirety?
使用jenkins之二Job
Pageoffice - bug modification journey
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
Confluence的PDF导出中文文档异常显示问题解决
setInterval定时器在ie不生效原因之一:回调的是箭头函数
为什么网站打开速度慢?
Bloom filter
随机推荐
Slf4j + Logback日志框架
Two common methods and steps of character device registration
多进程编程(二):管道
写论文可以去哪些网站搜索参考文献?
Architecture: load balancing
[pulsar document] concepts and architecture
redis21道经典面试题,极限拉扯面试官
Redis21 classic interview questions, extreme pull interviewer
毕业总结
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
Confluence的PDF导出中文文档异常显示问题解决
NC50965 Largest Rectangle in a Histogram
Pat 1030 travel plan (30 points) (unfinished)
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
Seckill system design
Should you study kubernetes?
Kubernetes simple introduction to writing YML
logback配置文件
教育学大佬是怎么找外文参考文献的?
Basic use of shell script