轻量ORM-SqlRepoEx (七)AspNetCore应用
ORM-SqlRepoEx 是 .Net平台下兼容.NET Standard 2.0,一个实现以Lambda表达式转转换标准SQL语句,使用强类型操作数据的轻量级ORM工具,在减少魔法字串同时,通过灵活的Lambda表达式组合,实现业务数据查询的多样性。
ORM-SqlRepoEx 也是一个极易使用的工具,通过在AspNetCore中的应用可以展示。
本案例源码在:
https://github.com/AzThinker/SqlRepoEx2.0DemoForAspCore
或
https://gitee.com/azthinker/SqlRepoEx2.0DemoForAspCore
源码部分代码是使用代码工具生成
https://github.com/AzThinker/CodeToolSolution
1、新建一个AspNetCore项目
2、通过Nuget下载SqlRepoEx库、由于本例中是AspNetCore.Mvc项目,案例中使用的是SQL Server的Northwind数据库,所以选择下载
SqlRepoEx.MsSql.ServiceCollection
3、在Startup.cs文件的public void ConfigureServices(IServiceCollection services)中添加
string ConnectionString = "Data Source=(Local);Initial Catalog=Northwind;User ID=test;Password=test";
services.AddSimpleSqlRepo(ConnectionString);
4、增加一个简单类AzCustomers,其属性来源于 Customers 表。为使SqlRepoEx 精准访问,增加特性标识 [TableName("Customers")] 。
using System;
using SqlRepoEx.Core.CustomAttribute; // 客户 业务类
namespace DemoTools.BLL.DemoNorthwind
{
[TableName("Customers")]
/// <summary>
/// 客户 业务类
/// </summary>
public sealed class AzCustomers
{
public string CustomerID { get; set; } public string CompanyName { get; set; } public string ContactName { get; set; } public string ContactTitle { get; set; } public string Address { get; set; } public string City { get; set; } public string Region { get; set; } public string PostalCode { get; set; } public string Country { get; set; } public string Phone { get; set; } public string Fax { get; set; } }
}
5、增加一人简单的列表类 AzCustomersList,其中实现了IPagedList接口,此接口是Webdiyer.WebControls.AspNetCore分页控件中定义,由于Webdiyer.WebControls.AspNetCore的源码不支持core2.1,所以重新编译,并将源码加工程中。
using System;
using System.Collections.Generic;
using Webdiyer.WebControls.AspNetCore; //客户列表类
namespace DemoTools.BLL.DemoNorthwind
{
/// <summary>
/// 客户 列表类
/// </summary>
public class AzCustomersList : List<AzCustomers>, IPagedList
{
public string DisplayDescription = "客户";
public int PageSize { get; set; }
public int TotalItemCount { get; set; }
public int CurrentPageIndex { get; set; } public static AzCustomersList GetModelList((IEnumerable<AzCustomers> QueryResult, int PageCount) queryresult, int pageSize, int currentPageIndex)
{
AzCustomersList models = new AzCustomersList();
models.AddRange(queryresult.QueryResult);
models.TotalItemCount = queryresult.PageCount;
models.PageSize = pageSize;
models.CurrentPageIndex = currentPageIndex;
return models;
}
}
}
6、增加一个控制器并在控制器的构造方法 AzCustomersController(IRepositoryFactory repositoryFactory),IRepositoryFactory是SqlRepoEx 工厂类的接口,由于前面(第2条中)已经注册了SqlRepoEx 所需的依赖,此处仅需在构造中加入此接口即可。
using System.Linq;
using DemoTools.BLL.DemoNorthwind;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlRepoEx.Abstractions; // 客户 控制器
namespace DemoTools.WebUI.DemoNorthwind.Controllers
{
/// <summary>
/// 客户
/// </summary>
public class AzCustomersController : Controller
{
IRepositoryFactory repositoryFactory;
IRepository<AzCustomers> repository;
public AzCustomersController(IRepositoryFactory repositoryFactory)
{
this.repositoryFactory = repositoryFactory;
this.repository = repositoryFactory.Create<AzCustomers>();
} /// <summary>
/// 返回 客户 列表
/// 异步调用数据,其异步部分明细View没有Controller只有View
/// </summary>
public IActionResult Index(int pageindex = )
{
var queryresult = repository.Query()
.Select(s => s.CustomerID
, s => s.CompanyName
, s => s.ContactName
, s => s.ContactTitle
, s => s.Address
, s => s.City
, s => s.Region
, s => s.PostalCode
, s => s.Country
, s => s.Phone
, s => s.Fax
).OrderBy(o => o.CustomerID).Page(, pageindex).PageGo();
var model = AzCustomersList.GetModelList(queryresult, , pageindex);
string xrh = Request.Headers["X-Requested-With"];
if (!string.IsNullOrEmpty(xrh) && xrh.Equals("XMLHttpRequest", System.StringComparison.OrdinalIgnoreCase))
{
return PartialView("DetailsPage", model);
}
return View(model);
} /// <summary>
/// 增加客户
/// </summary>
public ActionResult Create()
{
var model = new AzCustomers();
return View(model);
} /// <summary>
/// 增加保存客户
/// </summary>
[HttpPost, ValidateAntiForgeryToken]
[ActionName("Create")]
public IActionResult CreatePost(AzCustomers model)
{
if (ModelState.IsValid)
{
repository.Insert().With(s => s.CustomerID, model.CustomerID)
.With(s => s.CompanyName, model.CompanyName)
.With(s => s.ContactName, model.ContactName)
.With(s => s.ContactTitle, model.ContactTitle)
.With(s => s.Address, model.Address)
.With(s => s.City, model.City)
.With(s => s.Region, model.Region)
.With(s => s.PostalCode, model.PostalCode)
.With(s => s.Country, model.Country)
.With(s => s.Phone, model.Phone)
.With(s => s.Fax, model.Fax)
.Go();//按增加保存
return RedirectToAction("Index");
}
return View(model);
} /// <summary>
/// 编辑客户
/// </summary>
public IActionResult Edit(string Id)
{
var model = repository.Query()
.Select(s => s.CustomerID
, s => s.CompanyName
, s => s.ContactName
, s => s.ContactTitle
, s => s.Address
, s => s.City
, s => s.Region
, s => s.PostalCode
, s => s.Country
, s => s.Phone
, s => s.Fax
).Where(s => s.CustomerID == Id).Go().FirstOrDefault();
return View(model);
} /// <summary>
/// 保存编辑的客户
/// </summary>
[HttpPost, ValidateAntiForgeryToken]
[ActionName("Edit")]
public IActionResult EditPost(AzCustomers model)
{
if (ModelState.IsValid)
{
repository.Update().Set(s => s.CustomerID, model.CustomerID)
.Set(s => s.CompanyName, model.CompanyName)
.Set(s => s.ContactName, model.ContactName)
.Set(s => s.ContactTitle, model.ContactTitle)
.Set(s => s.Address, model.Address)
.Set(s => s.City, model.City)
.Set(s => s.Region, model.Region)
.Set(s => s.PostalCode, model.PostalCode)
.Set(s => s.Country, model.Country)
.Set(s => s.Phone, model.Phone)
.Set(s => s.Fax, model.Fax)
.Go();//按增加保存
return RedirectToAction("Index");
}
return View(model);
} /// <summary>
/// 显示客户单个记录
/// </summary>
public IActionResult Details(string Id)
{
var model = repository.Query()
.Select(s => s.CustomerID
, s => s.CompanyName
, s => s.ContactName
, s => s.ContactTitle
, s => s.Address
, s => s.City
, s => s.Region
, s => s.PostalCode
, s => s.Country
, s => s.Phone
, s => s.Fax
).Where(s => s.CustomerID == Id).Go().FirstOrDefault();
return View(model);
} /// <summary>
/// 独立页面删除客户
/// </summary>
public ActionResult Delete(string Id)
{
var model = repository.Query()
.Select(s => s.CustomerID
, s => s.CompanyName
, s => s.ContactName
, s => s.ContactTitle
, s => s.Address
, s => s.City
, s => s.Region
, s => s.PostalCode
, s => s.Country
, s => s.Phone
, s => s.Fax
).Where(s => s.CustomerID == Id).Go().FirstOrDefault();
return View(model);
} /// <summary>
/// 独立页面删除客户
/// </summary>
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(AzCustomers model)
{
repository.Delete().Where(c => c.CustomerID == model.CustomerID).Go();
return RedirectToAction("Index");
} }
}
7、View的实现和其他代码参见上面给出的地址中的源码。
总结:从上面看出,对特SqlRepoEx 所需要特定的操作,仅在第2、第3、第6中是必需的
(1)、引用SqlRepoEx.MsSql.ServiceCollection
(2)、 services.AddSimpleSqlRepo(ConnectionString);
(3)、 AzCustomersController(IRepositoryFactory repositoryFactory)
(4)、this.repository = repositoryFactory.Create<AzCustomers>();
然后就可以轻松的通过SqlRepoEx 访问数据库了。
轻量ORM-SqlRepoEx (七)AspNetCore应用的更多相关文章
- Dapper.NET——轻量ORM
Dapper.NET使用 http://www.cnblogs.com/yankliu-vip/p/4182892.html 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Da ...
- SqlSugar轻量ORM
蓝灯软件数据股份有限公司项目,代码开源. SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法. 学习列表 0.功能更新 1.SqlSuga ...
- Dapper.NET—轻量ORM
Dapper.NET使用 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2.2实体类. 3.使用方法 3.1 一 ...
- C# Dapper 轻量ORM调试对SQLServer
Dapper简介 Dapper只有一个代码文件,完全开源,你可以放在项目里的任何位置,来实现数据到对象的ORM操作,体积小速度快. 使用ORM的好处是增.删.改很快,不用自己写sql,因为这都是重复技 ...
- 轻量ORM-SqlRepoEx介绍
轻量级 ORM-SqlRepoEx 介绍 SqlRepoEx是 .Net平台下兼容.NET Standard 2.0人一个轻型的ORM.解决了Lambda转Sql语句这一难题,SqlRepoEx使用的 ...
- 轻量ORM-SqlRepoEx (九)与Dapper共舞
Dapper就另一个轻量ORM,Dapper及其扩展解决了数据访问端的大部门问题,提供了如数据事务管理.缓存等支持.SqlRepoEx的重点解决了Lambda转换成SQL语句,使SQL使用强类型编写, ...
- 轻量型ORM框架Dapper的使用
在真实的项目开发中,可能有些人比较喜欢写SQL语句,但是对于EF这种ORM框架比较排斥,那么轻量型的Dapper就是一个不错的选择,即让你写sql语句了,有进行了关系对象映射.其实对于EF吧,我说下我 ...
- OWIN轻量型框架介绍
OWIN轻量型框架介绍 阅读目录 引言 框架的特色 如何启动 各项功能 静态路由的3种写法 伪静态路由的支持 处理Form表单提交的文件 流式处理Post请求的数据 多种请求类型自动识别 响应处理 请 ...
- faked 一个用于 mock 后端 API 的轻量工具
一.简介 faked 是一个在前端开发中用于 mock 服务端接口的模块,轻量简单,无需要在本地启动 Server 也无需其它更多的资源,仅在浏览器中完成「请求拉截」,配合完整的「路由系统」轻而易举的 ...
随机推荐
- ASP.NET 后台验证 TextBox 值是否为数字
记得最开始 using System.Text.RegularExpressions; Regex m_regex = new System.Text.RegularExpressions.Regex ...
- JavaScript switch语句
JavaScriptswitch语句 switch语句用于基于不同的条件来执行不同的动作. JavaScript switch 语句 使用switch语句可以进行多项选择. 语法: switch( 变 ...
- [小北De编程手记] : Lesson 03 - Selenium For C# 之 元素定位
无论哪一种自动化测试的驱动框架(基于B/S,桌面应用,还是手机App).都应当具有一套优秀的元素定位技术.通常的自动化测试流程也可以简单的归结为是一个从被测试程序中识别或是定位元素以及执行操作和验证元 ...
- CentOS 7运维管理笔记(10)----MySQL源码安装
MySQL可以支持多种平台,如Windows,UNIX,FreeBSD或其他Linux系统.本篇随笔记录在CentOS 7 上使用源码安装MySQL的过程. 1.下载源码 选择使用北理工的镜像文件: ...
- Web.py报错:OSError: No socket could be created -- (('0.0.0.0', 8080):
web.py报错 Python代码: import web urls = ( '/(.*)', 'hello' ) app = web.application(urls, globals()) cla ...
- 错误的git reset操作之后的补救措施
(相关命令:git reset.git log.git reflog,要看文档的话用--help.) 这是一次愚蠢的行为之后的总结……避免我之后忘记了解决方法[逃 get reset --hard是一 ...
- 并发包交换数据Exchanger
/** * * @描述: 用于实现两个人之间的数据交换,每个人完成一定的事务后想与对方交换数据,第一个先拿出数据的人一直等待 * 直到第二个人拿到数据 到来时,才能彼此交换数据. * @作者: Wnj ...
- Excel Events
WorkbookEvents Interface WorkbookEvents_ActivateEventHandler Delegate WorkbookEvents_AddinInstallEve ...
- make menuconfig 出错解决
问题: hank@hank-virtual-machine:/opt/Emb/linux-2.6.30.4$ sudo make menuconfig *** Unable to find the n ...
- python UI自动化实战记录五:测试页面2 pageobject
该部分记录测试页面2-StrategyPage,所有页面2上的元素定位.操作.获取属性等方法都写在该类中. 1 页面2继承自BasePage: 2 页面2第一部分写的是所有的定位器 3 页面2第二部分 ...