Agile.DataAccess.dll 文件为系统平台数据访问支持库,基于FluentData扩展重写,提供高效的性能与风格简洁的API,支持多种主流数据库访问。

当前市面上的 ORM 框架,如 Entity Framework 和 NHibernate,都过于复杂而且难于学习。此外,由于这些框架自身抽象的查询语言以及从数据库到 .NET 对象的映射太过麻烦,导致它们生成的 SQL 都很低效。

Agile.DataAccess是一个轻量级框架,采用函数式编程风格以及简单的 API 并且很容易学会。与其他微型 ORM(如 Dapper 和 Massive)类似关注性能和易用性。它允许开发人员拥有对 SQL 较多的控制,而不是完全依赖 ORM 进行自动生成。它不仅可以使用 SQL 来执行查询、增添和更新操作,还可以支持使用存储过程和事务。可以在不改动已有结构的情况下,与任何业务对象一同工作。

以下是 Agile.DataAccess 的一些其他特性:  
  · 多结果集(Multiple Result Set):在一次数据库操作下返回多个数据集;
  · 开发人员可使用强类型对象或动态对象;
  · 可为创建时需要特殊处理的复杂对象自定义实体工厂(Custom Entity Factory);
  · 具有添加其他数据库支持的能力。

数据库支持列表:

MS SQL Server using the native .NET driver.

MS SQL Azure using the native .NET driver.

MS Access using the native .NET driver.

MS SQL Server Compact 4.0 driver.

Oracle through the ODP.NET driver.

MySQL through the MySQL Connector .NET driver.

SQLite through the SQLite ADO.NET Data Provider.

PostgreSql through the Npgsql provider.

IBM DB2 through the IBM DB2 .NET driver

Sybase through the Sybase provider.

API 示例

一、创建数据库上下文

方式一:创建默认DataBaseContext,ConnectionStrings["Default"]

var Context = new Agile.DataAccess.DataContext();

方式二:从指定的"ConnectionStrings[配置名]创建DataContext

var Context = new Agile.DataAccess.DataContext("Default");

方式三:从数据库连接字符串和数据库类型字符串名创建DataContext

var Context = new DataContext("Data Source=.;Initial Catalog=Agile;User Id=sa;Password=123;", "SqlServer");

方式四:从数据库连接字符串和数据库类型枚举创建DataContext

var Context = new DataContext("Data Source=.;Initial Catalog=Agile;User Id=sa;Password=123;", DataProvider.SqlServer);

二、执行T-SQL(Script)

查询返回动态对象列表:

List<dynamic> products = Context.Script("select * from Product").QueryMany<dynamic>();

查询返回强类型对象列表:

List<Product> products = Context.Script("select * from Product").QueryMany<Product>();

查询返回单个动态对象:

dynamic product = Context.Script("select * from Product where ProductId = 1").QuerySingle<dynamic>();

查询返回单个强类型对象:

Product product = Context.Script("select * from Product where ProductId = 1").QuerySingle<Product>();

查询返回数据表:

DataTable products = Context.Script("select * from Product").QuerySingle<DataTable>();

查询一个标量值:

int number = Context.Script("select count(*) from Product").QuerySingle<int>();

查询一个标量值的列表:

List<int> productIds = Context.Script("select ProductId from Product").QueryMany<int>();

自定义匹配实体类:

List<Product> products = Context.Script("select p.*,c.CategoryId as Category_CategoryId,c.Name as Category_Name
from Product p
join Category c on p.CategoryId = c.CategoryId").QueryMany<Product>();

自定义映射:

List<Product> products = Context.Script("select * from Product").QueryMany<Product>(Custom_mapper_dynamic);

public void Custom_mapper_dynamic(Product product, dynamic row)
{
product.ProductId = row.ProductId;
product.Name = row.Name;
}

自定义映射使用DataReader:

List<Product> products = Context.Script("select * from Product").QueryMany<Product>(Custom_mapper_datareader);

public void Custom_mapper_datareader(Product product, IDataReader row)
{
product.ProductId = row.GetInt32("ProductId");
product.Name = row.GetString("Name");
}

三、查询参数的应用(Parameter)

方式一:

dynamic products = Context.Script("select * from Product where ProductId = @0 or ProductId = @1")
.Parameters(, ).QueryMany<dynamic>();

方式二:(命名参数 1)

dynamic products = Context.Script("select * from Product where ProductId = @ProductId")                           
.Parameter("ProductId", )
.QueryMany<dynamic>();

方式二:(命名参数 2)

dynamic products = Context.Script("select * from Product where ProductId = {ProductId}")
.Parameter("ProductId", )
.QueryMany<dynamic>();

方式三:(输出参数)

var command = Context.Script("select @ProductName = Name from Product where ProductId = 1")
.ParameterOut("ProductName", DataTypes.String, );
command.Execute();
string productName = command.ParameterValue<string>("ProductName");

方式四:(IN参数)

List<int> productIds = new List<int>(){ 1, 2, 3 } ;
dynamic products = Context.Script("select * from Product where ProductId in (@ProductIds)")
.Parameter("ProductIds", productIds)
.QueryMany<dynamic>();

四、查询数据(Select)

查询返回动态对象列表:

List<dynamic> products = Context.Select("Product").QueryMany<dynamic>();

查询返回强类型对象列表:

List<Product> products = Context.Select("Product").QueryMany<Product>();

查询返回单个动态对象:

dynamic product = Context.Select("Product").Where("ProductId", ).QuerySingle<dynamic>();

查询返回单个强类型对象:

Product product = Context.Select("Product").Where("ProductId", 1).QuerySingle<Product>();

查询返回数据表:

DataTable product = Context.Select("Product").QuerySingle<DataTable>();

自动构造SQL

Product product = Context.Select<Product>().Where("ProductId", ).QuerySingle();

List<Product> products = Context.Select<Product>().QueryMany();

分页查询:

List<Product> products = Context.Builder<Product>().Select("p.*, c.Name as Category_Name")
.From("Product p Category c on c.CategoryId = p.CategoryId")
.Where("p.ProductId > 0 and p.Name is not null")
.OrderBy("p.Name")
.Paging(, )
.QueryMany();

五、插入数据(Insert)

方式一:

Context.Script("insert into Product(Name, CategoryId) values(@0, @1);").Parameters("Way", ).Execute();

方式二 :

Context.Script("insert into Product(Name, CategoryId) values(@Name, @CategoryId)")
.Parameter("Name", "Way")
.Parameter("CategoryId", )
.Execute();

方式三:(插入数据并返回自增主键)

int productId = Context.Script("insert into Product(Name, CategoryId) values(@0, @1);")
.Parameters("Way", ).ExecuteReturnLastId<int>();

方式四: (插入数据并返回自增主键)

int productId = Context.Script("insert into Product(Name, CategoryId) values(@Name, @CategoryId)")
.Parameter("Name", "Way")
.Parameter("CategoryId", )
.ExecuteReturnLastId<int>();

方式五:(自动构造SQL)

Context.Insert("Product").Column("Name", "The Warren Buffet Way").Column("CategoryId", ).Execute();

方式六: (自动构造SQL)

Product product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = ;
Context.Insert<Product>(product).AutoMap(x => x.ProductId).Execute();

六、更新数据(Update)

方式一:

int rowsAffected = Context.Script("update Product set Name = @0 where ProductId = @1")
.Parameters("The Warren Buffet Way", ).Execute();

方式二:

int rowsAffected = Context.Script("update Product set Name = @Name where ProductId = @ProductId")
.Parameter("Name", "The Warren Buffet Way")
.Parameter("ProductId" ,)
.Execute();

方式三:

int rowsAffected = Context.Update("Product")
.Column("Name", "The Warren Buffet Way")
.Where("ProductId", )
.Execute();

方式四: (自动构造SQL)

int rowsAffected = Context.Update<Product>(product)
.AutoMap(x => x.ProductId)
.Where(x => x.ProductId)
.Execute();

插入或更新补填方法:

var product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = ;
var insertBuilder = Context.Insert<Product>(product).Fill(FillBuilder);
var updateBuilder = Context.Update<Product>(product).Fill(FillBuilder);

public void FillBuilder(IInsertUpdateBuilder<Product> builder)
{
builder.Column(x => x.Name);
builder.Column(x => x.CategoryId);
}

七、删除数据(Delete)

方式一:

int rowsAffected = Context.Script("delete from Product where ProductId = 1").Execute();

方式二:

int rowsAffected = Context.Script("delete from Product where ProductId = @0").Parameters().Execute();

方式三:

int rowsAffected = Context.Script("delete from Product where ProductId = @ProductId ")
.Parameter("ProductId ", ).Execute();

方式四:(自动构造SQL)

int rowsAffected = Context.Delete("Product").Where("ProductId", ).Execute();

方式五:(自动构造SQL)

int rowsAffected = Context.Delete<Product>(product)
.Where(x => x.ProductId)
.Execute();

八、记录是否存在(Exists)

方式一:

bool result = Context.Exists("Product").Where("ProductId", 1).Execute();

方式二:

bool result = Context.Exists<Product>(product)
.Where(x => x.ProductId)
.Execute();

九、存储过程(Procedure)

方式一:

var rowsAffected = Context.Script("ProductUpdate")
.CommandType(DbCommandTypes.StoredProcedure)
.Parameter("ProductId", )
.Parameter("Name", "The Warren Buffet Way")
.Execute();

方式二:

var rowsAffected = Context.Procedure("ProductUpdate")
.Parameter("Name", "The Warren Buffet Way")
.Parameter("ProductId", ).Execute();

方式三:

var command = Context.Procedure("ProductUpdate")
.ParameterOut("Number", DataTypes.Int16)
.Parameter("Name", "The Warren Buffet Way")
.Parameter("ProductId", 1);
int rowsAffected = command.Execute();
int number = command.ParameterValue<int>("Number");

方式四:

var rowsAffected = Context.Procedure<Product>(product, "ProductUpdate").AutoMap(x => x.CategoryId).Execute();

方式五:

var rowsAffected = Context.Procedure<Product>(product,"ProductUpdate")
.Parameter(x => x.ProductId)
.Parameter(x => x.Name)
.Execute();

十、事物应用 (Transaction)

使用事务时,包装里面的代码使用using语句来确保数据库连接关闭。默认情况下,如果发生任何异常或者不提交事物将自动回滚。

using (var context = Context.BeginTransaction())
{
context.Script("update Product set Name = @0 where ProductId = @1")
.Parameters("The Warren Buffet Way", )
.Execute(); context.Update("Product")
.Column("Name", "The Warren Buffet Way")
.Where("ProductId", )
.Execute(); context.Update<Product>(product)
.AutoMap(x => x.ProductId)
.Where(x => x.ProductId)
.Execute(); context.Commit();
}

十一、共享链接 (SharedConnection)

使用共享链接时,包装里面的代码使用using语句来确保数据库连接关闭。

using (var context = Context.SharedConnection())
{
context.Script("insert into Product(Name, ProductId) values(@0, @1)")
.Parameters("The Warren Buffet Way", 1)
.Execute(); context.Insert("Product")
.Column("Name", "The Warren Buffet Way")
.Column("CategoryId", 1)
            .Execute(); context.Insert<Product>(product)
.AutoMap(x => x.ProductId)
            .Execute();
}

 开源地址:http://gitee.com/MuNet/AgileDataAccess

下载地址:http://files.cnblogs.com/files/MuNet/Agile.DataAccess.zip

Agile.Net 组件式开发平台 - 数据访问组件的更多相关文章

  1. Agile.Net 组件式开发平台 - 数据报表组件

    Agile.Report.dll 文件为平台数据报表支持库,基于FasstReport.Net扩展重写,提供了非常强大的自定义报表的功能使开发者为应用程序快速有效地生成报表.报表类库提供了创建报表所需 ...

  2. Agile.Net 组件式开发平台 - 内核管理组件

    敏捷开发体系   软件构件技术:所谓软件构件化,就是要让软件开发像机械制造工业一样,可以用各种标准和非标准的零件来进行组装.软件的构件化和集成技术的目标是:软件系统可以由不同厂商提供的,用不同语言开发 ...

  3. Agile.Net 组件式开发平台 - 脚本管理组件

    脚本管理组件用于管理系统查询脚本,由于数据查询的复杂性和可变性,平台规范要求使用查询使用建立在脚本管理器中的SQL语句.新增.更新.删除数据采用数据访问支持库API. 示例如下: 首先在脚本管理器中定 ...

  4. Agile.Net 组件式开发平台 - 权限管理组件

    RBAC原则       (1)最小权限原则之所以被RBAC所支持,是因为RBAC可以将其角色配置成其完成任务所需要的最小的权限集.       (2)责任分离原则可以通过调用相互独立互斥的角色来共同 ...

  5. Agile.Net 组件式开发平台 - 平台系统介绍

    平台介绍 Agile.Net 组件式开发平台是一款针对企业级产品的开发框架,平台架构基于SOA服务体系,多层组件式架构打造.平台提供企业应用开发所需的诸如ORM.IOC.WCF.EBS.SOA等分布式 ...

  6. Agile.Net 组件式开发平台 - 组件开发示例

    所谓组件式开发平台,它所有的功能模块都是以组件的形式扩展的,下面我来演示一个简单的组件开发例程. Agile.Net开发管理平台项目,已经托管在开源中国码云平台(http://git.oschina. ...

  7. Agile.Net 组件式开发平台 - 驱动开发示例

    首先讲一下概念,此驱动非彼驱动.在Agle.Net中我们将组件规划成两种类型,一种是基于业务的窗体组件,一种是提供扩展功能的驱动组件. 打个比方例如一般系统中需要提供身份证读卡功能,然而市面上有很多种 ...

  8. Agile.Net 组件式开发平台 - 服务开发示例

    在上一篇文章中已经讲解了组件的开发,这篇文章讲解平台服务开发. Agile.Net开发管理平台项目,已经托管在开源中国码云平台(http://git.oschina.net) 登陆码云平台进入项目主页 ...

  9. Agile.Net 组件式开发平台 - 系统文档中心

    Agile.Debgu.exe 文件为平台文档中心应用程序,该程序集成了数据库结构文档查询.数据库结构文档浏览.实时系统日志监控等功能.  数据库结构文档浏览 数据库结构文档查询 系统平台日志监控

随机推荐

  1. 神经网络环境搭建,windows上安装theano和keras的流程

    今天碰到有朋友问道怎么在windows下安装keras,正好我刚完成搭建,总结下过程,也算是一个教程吧,给有需要的朋友. 步骤一:安装python. 这一步没啥好说的,下载相应的python安装即可, ...

  2. 使用gcc编译gdb调试

    gcc 在linux系统中,默认是没有安装gcc编译器的,可以通过命令 rpm -q | grep gcc 来查看.安装命令为: yum -y install gcc 安装后,编写一个.c结尾的文件. ...

  3. IE 、Firefox、Chrome 浏览器在 F12 控制台下切换至不同框架介绍

    有不少网页的页面,还在使用 iframe 标签,而此时,相当于页面有两个 window 对象,一个为当前页面 window ,另一个则为 iframe 页面下的 window .因为,有时候需要在 c ...

  4. 教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构

    (40)Take advantage of .NET 4.5 async constructs 招数40: 利用.NET 4.5异步结构 With the arrival of .NET 4.5, w ...

  5. 处理get中的中文乱码情况

    1 最基本的乱码问题.这个乱码问题是最简单的乱码问题.一般新会出现.就是页面编码不一致导致的乱码.<%@ page language="java" pageEncoding= ...

  6. Android如何判断一个应用在运行

    在一个应用中,或一个Service .Receiver中有时候需要判断一个应用是否正在运行,以便进行一些相关的处理,这个时候我们需要得到一个ActivityManager,这个Manager顾名思意就 ...

  7. 在.NET中实现彩色光标/动画光标和自定义光标[转]

    下面是完整的例子,可以通过命令行编译即可看到效果. Test.cs using System; using System.Drawing; using System.Windows.Forms; us ...

  8. Unity3D脚本--经常使用代码集

    1. 訪问其他物体 1) 使用Find()和FindWithTag()命令 Find和FindWithTag是很耗费时间的命令,要避免在Update()中和每一帧都被调用的函数中使用.在Start() ...

  9. python selenium自动化(三)Chrome Webdriver的兼容

    当一个自动化测试被实现在一个浏览器之后,我们会希望我们的测试能够覆盖到尽量多的别的浏览器.通过跨平台的测试来保证我们的程序在多个浏览器下都能正常工作. 在安装了selenium之后,firefox w ...

  10. ABAP EXCEL 文件上传下载 用SMW0

    T-CODE: SMW0 在这里只介绍二进制数据,HTML模板的上传也一样. 另外也可以用CBO TABLE管理文件 可以看我另一个博文:CBO TABLE管理文件上传下载 选择 二进制 写包名: 进 ...