Agile.Net 组件式开发平台 - 数据访问组件

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(1, 2).QueryMany<dynamic>();

 

方式二:(命名参数 1)

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

  

方式二:(命名参数 2)

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

 

方式三:(输出参数)

var command = Context.Script("select @ProductName = Name from Product where ProductId = 1")
.ParameterOut("ProductName", DataTypes.String, 100); 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", 1).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", 1).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(1, 10)
                                                   .QueryMany();

 

 

五、插入数据(Insert) 

方式一:

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


方式二 :

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

 

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

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

 

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

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

 

方式五:(自动构造SQL)

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

 

方式六: (自动构造SQL)

Product product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = 1;
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", 1).Execute();

 

方式二:

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

 

方式三:

int rowsAffected = Context.Update("Product")
                          .Column("Name", "The Warren Buffet Way")
                          .Where("ProductId", 1)
                          .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 = 1;  
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(1).Execute();

 

方式三:

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


方式四:(自动构造SQL)

int rowsAffected = Context.Delete("Product").Where("ProductId", 1).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", 1)
                          .Parameter("Name", "The Warren Buffet Way")
                          .Execute();

 

方式二:

var rowsAffected = Context.Procedure("ProductUpdate")
                          .Parameter("Name", "The Warren Buffet Way")
                          .Parameter("ProductId", 1).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", 1)
            .Execute();
 
     context.Update("Product")             
            .Column("Name", "The Warren Buffet Way")
            .Where("ProductId", 1)
            .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

 

转载于:https://www.cnblogs.com/MuNet/p/5740833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Agile.net Code Protection - Powerful .NET Code Protection & Obfuscation Solution Agile.NET code protection solution offers better protection for your .NET code by providing a protection that goes beyond standard obfuscation methods. Our advanced code protection technology delivers a layered protection approach that combines our patented code virtualization technology, code encryption, obfuscation, and anti-reverse engineering countermeasures embedded into existing application code. Unbreakable Code Protection Agile.net uses a patented technology that transforms MSIL code into virtual opcodes that will only be understood by a secure virtual machine. The virtual machine processes the virtual opcodes directly, emulating the original code behavior without transforming the code back to its original form. Breaking the protection becomes NP-complete problem, thus making Agile.net the only obfuscator that guarantees your code can't be broken! Next Generation Obfuscator Obfuscates every aspect of your code, including class and method names, managed resources, user strings, methods implementation, system and library calls. Agile.net Protects more of your application than any other obfuscator because it knows what is safe to change and what needs to be left alone. Nevertheless, it gives you full control of the obfuscation process. Works with all .NET Technologies Join our rapidly growing customer base consisting of thousands of customers using Agile.net every day to protect their software running on WinRT, WP7, WP8, .NET 2.0 and up, Compact Framework 2.0 and up, ASP. NET, Silverlight 2.0 and up (XAML), WPF (BAML), XNA, ClickOnce and more
agile.net_obfuscator_6.6.0.11_downloadly是一个用于.NET软件开发的混淆器。混淆器是一种用于加密和保护软件源代码的工具。它会对代码进行各种转换和修改,使得人们很难阅读和理解源代码。这种技术可以有效地保护软件不被未经授权的访问或盗取。 agile.net_obfuscator_6.6.0.11_downloadly有一些主要的功能和特点。首先,它可以对.NET程序集进行混淆和优化,以降低源代码泄露和反编译的风险。混淆后的代码会变得难以理解,使攻击者很难分析和修改。其次,它可以隐藏敏感信息,如字符串和方法名称,以增加应用程序的安全性。 除了代码混淆,agile.net_obfuscator_6.6.0.11_downloadly还提供了其他的安全功能。例如,它可以对程序集进行数字签名,确保代码和应用程序在传输过程中不被篡改。它还提供了反调试和反追踪功能,防止黑客对应用程序进行逆向工程和调试。此外,它还支持多种混淆方案和自定义规则,以满足开发者对安全性和性能的不同需求。 agile.net_obfuscator_6.6.0.11_downloadly是一个专业级的工具,适用于中小型软件开发团队和独立开发者。它可以帮助开发者保护知识产权,防止程序被盗用和修改。虽然它可以提供一定的安全保护,但也无法100%阻止攻击,因此在使用该工具时,开发者还应该采取其他安全措施,如加密和权限管理,以确保应用程序的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值