Introduction to FluentData - The Micro ORM

37
Lars-Erik Kindblad Senior Consultant Blog: kindblad.com Introduction to FluentData

description

Lars-Erik Kindblad presentation at the Capgemini Microsoft User Group 2012.

Transcript of Introduction to FluentData - The Micro ORM

Page 1: Introduction to FluentData - The Micro ORM

Lars-Erik KindbladSenior ConsultantBlog: kindblad.com

Introduction to FluentData

Page 2: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Agenda

What is FluentData? Why was FluentData created? Architecture Sample code Road map

Page 3: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

What is FluentData?

Micro ORM for .NET Makes it simple to query a relational database Open Source MIT license http://fluentdata.codeplex.com NuGet: FluentData

Page 4: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Why was FluentData created? ORMs such as Entity Framework and NHibernate have too many

disadvantages• Framework is too complex• Forces you to design the .NET Model in a special way by using attributes and

ORM Framework specific types• Hard to define the mapping between the database model and business model• A lot of mapping code/XML – not any less code than SQL and manual mapping• Uses an abstracted query language instead of SQLoHard to do complex queries, Generates poor SQL, Slow

• Takes a long time to learn and master• Drag and drop designers are buggy and limiting• = Leads to unpredictable development time and performance

... had to be a better way to do it

Page 5: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Alternatives to ORMs?

ADO.NET• Powerful• Flexible• Great performance• ... but too much manual code

Page 6: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

A Better Alternative?

ORM ADO.NET

SecureAuto mapping

FastFlexiblePredictableManual mapping

Micro ORM

SimpleFastFlexiblePredictableSecureAuto mappingManual mapping

Page 7: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Micro ORM

Uses SQL – the native and optimized query language for databases, rock solid – in use since the 70s

Thin wrapper layer on top of ADO.NET:• Reduce repetitive code• Support auto mapping

Map per query instead of per Entity

= Result• Fast• Simple• Efficient

Page 8: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Goals of FluentData

Easy to learn Simple and efficient to use Fast Secure Single DLL Only the Data Access Layer should be affected by FluentData Select queries should be SQL, insert, update and delete queries can

be SQL or generated Support the major databases

Page 9: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Features

Fluent API Supports SQL Server, SQL Server Compact, SQL Azure, Access,

MySQL, Oracle Use SQL to select data and builders to insert, update, delete data Supports

• Auto mapping and custom mapping• Stored Procedures• Transactions• Multiple result sets• Custom collections eg. ProductCollection instead of List<Product>• Provider model• ++

Page 10: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Architecture

Page 11: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

DbContext

Starting point for working with FluentData Use it to define settings such as

• ConnectionString or ConnectionStringName• UseTransaction

Hook up your own method to any of the supported events• OnConnectionOpened• OnConnectionOpening• OnConnectionClosed• OnExecuting• OnExecuted• OnError

Page 12: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

DbCommand

Responsible for performing the actually query against the database

Page 13: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Mapping

FluentData can automap query results to your own entity type or to a dynamic type (new in .NET 4.0)

or you can map the query results manually

Page 14: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Builders

Fluent API to generate SQL for insert, update and delete queries

Page 15: Introduction to FluentData - The Micro ORM

Sample Code

Page 16: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Getting started

Download FluentData.dll from either• FluentData.CodePlex.com• NuGet

Add a reference to FluentData.dll in your Data Access Project

Page 17: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

ModelDatabase

.NET

Page 18: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Create and Initialize the DbContext

Page 19: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

INSERT DATA

Page 20: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Insert a Category

Builder

SQL

Page 21: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

UPDATE DATA

Page 22: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Update a Product

Page 23: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

DELETE DATA

Page 24: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Delete a Product

Page 25: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

SELECT DATA

Page 26: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Select a Category

Page 27: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Select all Categories

Map to List<Category>

Map to CategoryCollection

Page 28: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Select all Products with the related Category

Auto Mapping

Page 29: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Select all Products with the related Category

Manual Mapping

Page 30: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Count the number of Products

Page 31: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Select All Categories + All Products

Page 32: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Select Products 1,2,3,4

Page 33: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

TRANSACTIONS

Page 34: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Insert multiple products within a Transaction

Page 35: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

Road map

Add support for more databases• SQLite, PostgreSQL +

Add a Select Builder with Paging support and strongly typed auto mapping

Simplify code Single Source File Performance improvements .NET intellisense for the database model

Page 36: Introduction to FluentData - The Micro ORM

| Sector, Alliance, Offering

QUESTIONS?

Page 37: Introduction to FluentData - The Micro ORM

www.capgemini.com

The information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved