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);
· 具有添加其他數據庫支持的能力。
FluentData 需要 .NET 4.0,並支持 SQL Server、SQL Azure、SQL Server Compact 以及使用 .NET 驅動的 Oracle 和 MySQL。 想要了解進一步信息,如代碼示例和免費下載,請訪問CodePlex 站點上的 FluentData。(http://fluentdata.codeplex.com/)
快速上手如何使用FluentData
下面我將一一舉例向大家介紹FluentData在開發過程中的運用.
一:下載該項目並且引用FluentData.dll,或者直接在解決方案中添加該開源項目.項目地址:http://fluentdata.codeplex.com/
二.dll引用入到我們的數據業務層.
1.)創建並且初始化一個IDbContext.
它是我們與數據庫操作中的上下文,所有的有關數據操作都調用它下面的方法。初始化它的連接字符串web.config
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了。
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.返回一個泛型。
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>() }