FluentData入门(一)--核心概念

by kevin 7. 九月 2013 13:17 >

DbContext类

这是FluentData的核心类,可以通过配置ConnectionString来定义这个类,如何连接数据库和对具体的哪个数据库进行数据查询操作。

DbCommand类

这个类负责在相对应的数据库执行具体的每一个数据操作。

Events
DbContext类定义了以下这些事件:

  • OnConnectionClosed
  • OnConnectionOpened
  • OnConnectionOpening
  • OnError
  • OnExecuted
  • OnExecuting

可以在事件中,记录每个SQL查询错误或者SQL查询执行的 时间等信息。
Builders
Builder用来创建Insert, Update, Delete等相关的DbCommand实例。 
Mapping

FluentData可以将SQL查询结果自动映射成一个POCO(POCO - Plain Old CLR Object)实体类,也可以转换成一个dynamic类型。

自动转成实体类:

  1. 如果字段名中不包含下划线("_"),将映射到具有相同名字的属性上,例如:字段名 "Name" 将映射到属性名 "Name"。
  2. 如果字段名中不包含下划线("_"),将映射到内嵌的属性上,例如:字段名 "CategoryName" 将映射到属性名 "Category.Name"。

如果数据库字段名和属性名不一致,可以使用SQL的as让他们一致。


自动转换成dynamic类型:

  1. 对应dynamic类型,会为每个字段生成一个同名的属性,例如:字段名 "Name" 将映射到属性名 "Name"。
什么时候应该主动释放资源?
  • 如果使用UseTransaction或者UseSharedConnection,那么DbContext需要主动释放。
  • 如果使用UseMultiResult或者MultiResultSql,那么DbCommand需要主动释放。
  • 如果使用UseMultiResult,那么StoredProcedureBuilder需要主动释放。

其他所有的类都会自动释放,也就是说,一个数据库连接只在查询开始前才进行,查询结束后会马上关闭。

 

DbContext
This class is the starting point for working with FluentData. It has properties for defining configurations such as the connection string to the database, and operations for querying the database.

DbCommand
This is the class that is responsible for performing the actual query against the database.

Events
The DbContext class has support for the following events:
OnConnectionClosed
OnConnectionOpened
OnConnectionOpening
OnError
OnExecuted
OnExecuting
By using any of these then you can for instance write to the log if an error has occurred or when a query has been executed.

Builders
A builder provides a nice fluent API for generating SQL for insert, update and delete queries.

Mapping
FluentData can automap the result from a SQL query to either a dynamic type (new in .NET 4.0) or to your own .NET entity type (POCO - Plain Old CLR Object) by using the following convention:

Automap to an entity type:
If the field name does not contain an underscore ("_") then it will try to try to automap to a property with the same name. For instance a field named "Name" would be automapped to a property also named "Name".
If a field name does contain an underscore ("") then it will try to map to a nested property. For instance a field named "CategoryName" would be automapped to the property "Category.Name".
If there is a mismatch between the fields in the database and in the entity type then the alias keyword in SQL can be used or you can create your own mapping method. Check the mapping section below for code samples.

Automap to a dynamic type:
For dynamic types every field will be automapped to a property with the same name. For instance the field name Name would be automapped to the Name property.

When should you dispose?
DbContext must be disposed if you have enabled UseTransaction or UseSharedConnection.
DbCommand must be disposed if you have enabled UseMultiResult (or MultiResultSql).
StoredProcedureBuilder must be disposed if you have enabled UseMultiResult.
In all the other cases dispose will be handled automatically by FluentData. This means that a database connection is opened just before a query is executed and closed just after the execution has been completed.

分享到: 更多