当前位置:网站首页>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));
}
}


边栏推荐
- pageoffice-之bug修改之旅
- [target detection] r-cnn, fast r-cnn, fast r-cnn learning
- Custom throttling function six steps to deal with complex requirements
- JS interviewer wants to know how much you understand call, apply, bind no regrets series
- Introduction of UART, RS232, RS485, I2C and SPI
- Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
- 多进程编程(五):信号量
- 可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
- Multi process programming (III): message queue
- One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
猜你喜欢

Automated defect analysis in electron microscopic images-论文阅读笔记

ftrace工具的介绍及使用

Gan model architecture in mm

JS interviewer wants to know how much you understand call, apply, bind no regrets series

秒杀系统设计

Feature Engineering: summary of common feature transformation methods
![Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation](/img/4a/ab732c41ea8a939fa0983fec475622.png)
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation

Basic 10 of C language: array and pointer
![洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举](/img/a3/55bb71d39801ceeee421a0c8ded333.png)
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举

Chapter 3 of getting started with MySQL: database creation and operation
随机推荐
TypeError: Cannot read properties of undefined (reading ***)
Don't want teachers to see themselves with cameras in online classes? Virtual camera you deserve!
Markdown tutorial
Markdown使用教程
Briefly talk about other uses of operation and maintenance monitoring
大学生课堂作业2000~3000字的小论文,标准格式是什么?
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
Multiprocess programming (4): shared memory
Bloom filter
Bypass AV with golang
国外的论文在那找?
Kubernetes simple introduction to writing YML
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
setInterval定时器在ie不生效原因之一:回调的是箭头函数
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
What are the recommended thesis translation software?
AcWing_188. 武士风度的牛_bfs
Array de duplication