FluentData:一种使用Fluent API的新型轻量级ORM模型
FluentData 是微型 ORM(micro-ORM)家族的一名新成员,旨在比大型 ORM(full ORM)更加易用。FluentData 于本月推出,它使用 fluent API 并支持 SQL Server、SQL Azure、Oracle 和 MYSQL。
FluentData 的设计者 Lars-Erik Kindblad 谈到:
当前市面上的 ORM 框架,如 Entity Framework 和 NHibernate,都过于复杂并且难于学习。此外,因为这些框架自身抽象的查询语言以及从数据库到 .NET 对象的映射太过麻烦,致使它们生成的 SQL 都很低效。
FluentData 另辟蹊径,它是一个轻量级框架,拥有简单的 fluent API 而且很容易学会。
与其余微型 ORM(如 Dapper 和 Massive)相似,FluentData 关注性能和易用性。它容许开发人员拥有对 SQL 较多的控制,而不是依赖 ORM 进行自动生成。它不只可使用 SQL 来执行查询、增添和更新操做,还能够支持使用存储过程和事务。根据文档描述,FluentData 能够在不改动已有结构的状况下,与任何业务对象一同工做。
如下是 FluentData 的一些其余特性:
· 多结果集(Multiple Result Set):在一次数据库操做下返回多个数据集;
· 开发人员可以使用强类型对象或动态对象;
· 可为建立时须要特殊处理的复杂对象自定义实体工厂(Custom Entity Factory);
· 具备添加其余数据库支持的能力。 html
FluentData 须要 .NET 4.0,并支持 SQL Server、SQL Azure、SQL Server Compact 以及使用 .NET 驱动的 Oracle 和 MySQL。 想要了解进一步信息,如代码示例和免费下载,请访问CodePlex 站点上的 FluentData。(http://fluentdata.codeplex.com/)web
快速上手如何使用FluentData
下面我将一一举例向你们介绍FluentData在开发过程当中的运用. sql
一:下载该项目而且引用FluentData.dll,或者直接在解决方案中添加该开源项目.项目地址:http://fluentdata.codeplex.com/ 数据库
二.dll引用入到咱们的数据业务层.
1.)建立而且初始化一个IDbContext.
它是咱们与数据库操做中的上下文,全部的有关数据操做都调用它下面的方法。初始化它的链接字符串web.config app
public static IDbContext QueryDB() { return new DbContext().ConnectionStringName(\"testDBContext\", DbProviderTypes.SqlServer); }
2.)config中的链接字符串实例 框架
<connectionStrings> <add name=\"testDBContext\"connectionString=\"server=192.168.1.100;uid=sa;pwd=sa!;database=testDB;\" /> </connectionStrings>
那么下面就能够在咱们的数据业务层中根据本身的需求为所欲为的写sql了。 ide
1.须要返回一个实体:
性能
Product product = QueryDB().Sql(@\"select * from Product where ProductId = 1\").QuerySingle<Product>()
2.根据参数返回一个实体?别急,尝尝那飘渺的链式操做吧 学习
Product product = QueryDB().Sql(\"select * from Product where id=@id\") .Parameter(\"id\", id) .QuerySingle<Product>()
3.返回一个泛型。 ui
List<Product> product = QueryDB().Sql(\"select * from Product where id=@id\") .Parameter(\"id\", id) .Query<Product>()
4.多表支持(这个楼主实际工做中却是没有用到过)
using (var command = QueryDB().MultiResultSql()) { List<Category> categories = command.Sql( @\"select * from Category; select * from Product;\").Query<Category>(); List<Product> products = command.Query<Product>(); }
5.插入操做
var productId = QueryDB().Insert(\"Product\") .Column(\"Name\", \"The Warren Buffet Way\") .Column(\"CategoryId\", 1) .ExecuteReturnLastId()
6.固然我喜欢写我牛B的sql。
var productId = QueryDB().Sql(@\"insert into Product(Name, CategoryId) values(\‘The Warren Buffet Way\‘, 1);\").ExecuteReturnLastId()
7.修改操做.
QueryDB().Update(\"Product\") .Column(\"Name\", \"The Warren Buffet Way\") .Column(\"CategoryId\", 1) .Where(\"ProductId\", 1) .Execute()
同上,也能够不用update()方法,而直接写sql.
8.删除操做
QueryDB().Delete("Product").Where("ProductId", 1).Execute();
9.我想链式操做,我想写lambda表达式OK。
QueryDB().Delete<Product>(\"Product\") .Where(x=>x.id,id) .Execute()
10.事物的处理
using (var context = QueryDB().UseTransaction) { context.Sql(\"update Product set Name = @0 where ProductId = @1\") .Parameters(\"The Warren Buffet Way\", 1) .Execute(); context.Sql(\"update Product set Name = @0 where ProductId = @1\") .Parameters(\"Bill Gates Bio\", 2) .Execute(); context.Commit(); }
在事物的操做中记得context.Commit();方法的执行,楼主曾经在本身的一个项目中须要用到事物,却忘记了执行提交这个方法,最后在源码的汪 洋中探索许久
11.存储过程
有关存储过程的使用,楼主在实际项目开发中,用上了存储过程。该存储过程的做用是分页,那么这里也贴出来分享一下
public static List<T> getPage<T>(string tableName,string tableFields, string sqlWhere,string order,intpageIndex, int pageSize, out int total) { var store = QueryDB().StoredProcedure(\"PF_Sys_PageControl\") .ParameterOut(\"totalPage\", DataTypes.Int16) .Parameter(\"tableName\", tableName) .Parameter(\"tableFields\", tableFields) .Parameter(\"sqlWhere\", sqlWhere) .Parameter(\"orderFields\", order) .Parameter(\"pageSize\", pageSize) .Parameter(\"pageIndex\", pageIndex); var result=store.Query<T>() }