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


边栏推荐
- Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
- 论文的设计方案咋写?
- Nc20806 District interval
- NC17059 队列Q
- mm中的GAN模型架构
- TypeError: Cannot read properties of undefined (reading ***)
- pod生命周期详解
- 多进程编程(四):共享内存
- Mutual exclusion and synchronization of threads
- What are the recommended thesis translation software?
猜你喜欢

CMake基本使用

sysdig分析容器系统调用

MySQL advanced learning notes (III)

为什么网站打开速度慢?

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

Basic use of shell script

Should you study kubernetes?

Monitor container runtime tool Falco

请问大家在什么网站上能查到英文文献?

Seckill system design
随机推荐
Bloom filter
Sysdig analysis container system call
多进程编程(一):基本概念
Confluence的PDF导出中文文档异常显示问题解决
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
Implement the foreach method of array
JSON conversion tool class
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
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
Install docker and use docker to install MySQL
Redis21 classic interview questions, extreme pull interviewer
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
Multi process programming (III): message queue
Centos7 one click compilation to build MySQL script
NC20806 区区区间间间
form表单实例化
国外的论文在那找?
Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
Basic use of shell script
Which websites can I search for references when writing a thesis?