sses of the model:
[TableName("Categories")]
public class Category
{
[PrimaryKey, Identity] public int CategoryID;
[NotNull] public string CategoryName;
public string Description;
public Binary Picture;
[Association(ThisKey="CategoryID", OtherKey="CategoryID")]
public List
Products;
}
|
In addition to classes, we need a data model class, combining all the table model.
class NorthwindDB : DbManager
{
public NorthwindDB() : base("Northwind")
{
}
public Table
Category { get { return GetTable
(); } } // ... }
|
The existence of such a class is not a requirement, you can use our model by calling GetTable in place, but the presence of a class NorthwindDB generally make our code cleaner and easier to read.
In the future, we will slightly alter the description of our model in order to clarify some aspects of the work BLToolkit, but now it is enough to get started.
Ideology and programming model
BLToolkit ORM is not in the classic sense of the definition. Classic ORM solve two problems are essentially provided in one package: Data Mapping and Entity Services. The first task - it is just something that should be dealt with initially ORM system - shifting data from the database into objects and back. Second, Entity Services - a set of management services object model such as: ensuring referential integrity, object caching, change tracking, validation objects, etc. Recently, such ORM was called Heavy ORM (heavy ORM), and the tool provides only Data Mapping - Lightweight ORM (lightweight ORM).BLToolkit the second category. Therefore, you will not see BLToolkit:
automatic recovery operations nested lists of objects and lazy loading;automatic caching of objects;data context (like Linq to SQL DataContext), which tracks the changes and referential integrity has been read from the database objects;SubmitChanges operation for the data context.
Instead, you can access:
operations DML (Insert / Update / Delete) in a form in which they are present in SQL;the approximate maximum to SQL programming model, including including SELECT, the data source (for example, to generate a call SELECT CURRENT_TIMESTAMP);the ability to describe the data model using only interfaces;mechanisms for reusing expressions and subqueries.
In other words, BLToolkit will not hide from you try to work with the database of the application object model, but on the contrary, provide the most complete and natural means to work with your database.
Standard Linq queries
The first acquaintance
Our first test will be unremarkable. Here we just take the Employees table and perform her request.
static void FirstTest()
{
using (var db = new NorthwindDB())
{
var query = db.Employee;
foreach (var employee in query)
{
Console.WriteLine("{0} {1}", employee.EmployeeID, employee.FirstName);
}
}
} |
What could be simpler? However, let's look at what we have under the microscope. We set a breakpoint inside the loop and look at the contents of the variable query and db in the debugger.
?

?
First of all we are interested in the property SqlText variable query. Open Text visualizer and see the contents of SqlText.
?

?
This query text generated BLToolkit. The first line commented out the current line configuration data provider name and name of the SQL provider.
But, unfortunately, the property SqlText available only if we work with variable IQueriable
as in the previous case. It is not always possible, in the following example query returns