introduction to simple.data
DESCRIPTION
Simple.Data is a lightweight, dynamic data access component for .NET, written in C#. Find more about it at https://github.com/markrendle/Simple.Data. This Slide deck (in English) was used at the HerbstCampus and DWX Conferences in Nürnberg, Germany in July and September 2013 and provides you with an overview of the basic elements of the framework. The presentation material as well as the demo code can be found on github at https://github.com/Timothep/Talk.Simple.DataTRANSCRIPT
![Page 1: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/1.jpg)
1
Simple.Data
… an ORM without O, R or M
Timothée Bourguignon
![Page 2: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/2.jpg)
3
What is Simple.Data?
An O/RM without O, R or M!
![Page 3: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/3.jpg)
4
• SQL Server + MvcMusicStore DB– http://mvcmusicstore.codeplex.com/
Hands-on!
![Page 4: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/4.jpg)
5
What is Simple.Data?
• Lightweight way of manipulating data– Based on .NET 4.0's „dynamic“ keyword– Interprets method and property names– Maps them to your underlying data-store– Prevents SQL Injection– Inspired by Ruby’s ActiveRecord and DataMappers– Open Source & runs on Mono– V1.0 rc3 released in Nov. 2012
![Page 5: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/5.jpg)
6
The Menu
• Generalities• Conventions• CRUD Operations• Objects Returned• Joins & Evaluation Strategies• Various Functions• Tool & Testing• Wrap-Up
• Hands-onsalong the way
![Page 6: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/6.jpg)
7
Database agnostic
![Page 7: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/7.jpg)
9
MongoDB OData Azure Ado
Core
SqlServerSqlCompact40
OracleVistaDBMySQLSQLite
PostgreSQLSQLAnywhere
InformixMocking
Package Architecture
![Page 8: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/8.jpg)
10
Nuget Packages
PM> Install-Package Simple.Data.SqlServer Simple.Data.Ado Simple.Data.SqlCompact40 Simple.Data.Sqlite Simple.Data.MongoDB ...
![Page 9: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/9.jpg)
11
Conventions
![Page 10: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/10.jpg)
12
Opening a Connection
• Per default connections are aggressively opened and closed for each query
• Supports shared connections
var magicDb = Database.OpenConnection("ConnectString");
var fileDb = Database.OpenConnection("MyDB.sdf");
![Page 11: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/11.jpg)
13
Choose your weapon
• The „Fluid“ Way– Methods & properties convention-based mapping
• The „Indexer“ Way– Identification via an indexer syntax
![Page 12: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/12.jpg)
14
The Fluid Way
db.album.FindAllByGenreId(3);
Table/View
Comm
and
Colum
n
Param
eters
![Page 13: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/13.jpg)
15
The Indexer Way
//Full indexer waydb["sweets"].FindAllBy(Candy: "Oreo");//Hybrid fluid + indexerdb.sweets.FindAllBy(Candy: "Oreo");db["city"].FindAllByCityName("Paris");
//Find by „Candy“ or find by „C and Y“?db.sweets.FindAllByCAndY("Oreo");
• The problem
• The solution
![Page 14: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/14.jpg)
16
– Albums.GenreId– Album.GenreId– ALBUMS.GENREID– ALBUM.GENREID– [ALBUMS].[GENREID]
– [ALBUM].[GENREID]– AlBuM.geNReId– Al__*bum.Genr-eId– ...
• Sequence– Exact match– Case-insensitive - non-alphanumeric chars– Pluralized/Singularized version
• The following are thus all identical
Target Matching
![Page 15: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/15.jpg)
17
No IntelliSence
• Dynamics => no member / function inferrence• Schema analyse planned for Simple.Data v2• Tool: Simple.Data.Pad• Still easy to get used to
![Page 16: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/16.jpg)
18
CRUD OPERATIONS
![Page 17: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/17.jpg)
19
Create
• Insert(object or named parameters)
![Page 18: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/18.jpg)
20
Read
• Read– All()– Find(simple expressions)– Get(primary key)– FindAll(optional condition)– FindAllByXXX(parameter)
![Page 19: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/19.jpg)
21
Update
• Update(object or named parameters)– Update– UpdateByXXX– UpdateAll + optional condition
• Upsert e.g. Update or Insert• Some kind of optimistic locking
– Update(modifiedObject, originalObject)– Fails if the column(s) you are modifying changed
• Nota: does not work with Upsert
![Page 20: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/20.jpg)
22
Delete
• Delete– Delete(object or named parameters)– DeleteByXXX(parameters)– DeleteAll(optional conditions)
![Page 21: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/21.jpg)
23
Hands-on!
• CRUD Operations– Insert– Update– Delete– Read
![Page 22: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/22.jpg)
24
Objects Returned
![Page 23: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/23.jpg)
25
SimpleRecord
• Dynamic object• Contains a property for each of the columns
requested whose values are those of the single row retrieved from the data store
• „Cast-able“ to a concrete implementation
![Page 24: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/24.jpg)
26
SimpleQuery
• Dynamic object• Similar to LINQ structure• Executes when enumerated• Contains a SimpleRecord object for each row
returned
![Page 25: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/25.jpg)
27
Casting
• Casting to objects– Implicit– Explicit: Cast<T>, ToList, ToList<T>, ToArray,
ToArray<T>
• Hands-On– Implicit casting– Explicit casting
![Page 26: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/26.jpg)
28
Joins & Evaluation Strategies
![Page 27: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/27.jpg)
29
Hands-on!
• Lazy natural evaluation• Casting + Lazy?• Eager evaluation
![Page 28: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/28.jpg)
30
• Lazy loading– Natural Joins / Table chaining
• Eager Loading– „With Joins“
• With/WithXXX• WithOne• WithMany
– „Explicit Joins“• Join• LeftJoin• OuterJoin
Natural joins can be used as part of an explicit join, the join is then eager loaded
Foreign-Key relationship present
No Foreign-Key relationship necessary
(no referential integrity)
Joins
![Page 29: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/29.jpg)
31
Hands-on!
• Eager Joins– Select + Natural Joins + As– With
![Page 30: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/30.jpg)
32
Various Functions
![Page 31: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/31.jpg)
33
Ordering Results
• OrderBy, OrderByDescending• ThenBy, ThenByDescending
db.Albums.All().OrderByGenreId() .ThenByArtistIdDescending();
![Page 32: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/32.jpg)
34
Scalar Queries
• GetCount• GetCountBy• Exists, Any• ExistsBy, AnyBy
int albumCount = db.Albums.GetCount(db.Albums.GenreId == 2);
![Page 33: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/33.jpg)
35
Query Modifiers
• Select– Star & AllColumns
db.Albums.All().Select(db.Albums.Title, db.Albums.ArtistId);
![Page 34: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/34.jpg)
36
Query Modifiers
• Column Aliasing: As(string)
var albums = db.Albums.All().Select(db.Albums.AlbumId, db.Albums.Title.As("AlbumName"));
![Page 35: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/35.jpg)
37
Query Modifiers
• Where clauses– Operators (+, -, *, /, %)– IN, BETWEEN, LIKE, IS NULL
var albums = db.Albums.FindAllByGenreId(1).Select(db.Albums.Title).Where(db.Albums.Price < 8);
db.Albums.All().Where(db.Albums.Title.Like("%Side Of The%"));
![Page 36: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/36.jpg)
38
Aggregate Functions
• Grouping and Aggregates– Having → Group By / Having– Min, Max, Avg, Sum
var cheapAlbums = db.Albums.All().Having(db.Albums.Price < 9).ToList();
var totalCost = db.Albums.All().Select(db.Albums.Price.Sum().As("TotalCost"));
![Page 37: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/37.jpg)
39
Stored Procedures
CREATE PROCEDURE ProcedureWithParameters @One VARCHAR(MAX),@Two VARCHAR(MAX)AS
SELECT * FROM Customers WHERE Firstname = @One and Lastname like @Two
db.ProcedureWithParameters(1, 2);
• Like a function...
![Page 38: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/38.jpg)
40
Transactions
using (var transaction = db.BeginTransaction()){
transaction.albums.Insert(GenreId: 1...);transaction.Commit();
}
• Wrap up the calls
![Page 39: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/39.jpg)
41
Tool & Testing
![Page 40: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/40.jpg)
42
Tool: Simple.Data.Pad
• Similar to LINQ-Pad... kind of...– https://github.com/markrendle/Simple.Data.Pad
![Page 41: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/41.jpg)
43
Testing: InMemoryAdapter
[Test]public void Should_do_something(){
var adapter = new InMemoryAdapter();Database.UseMockAdapter(adapter);
var db = Database.Open(); db.Test.Insert(Id: 1, Name: "Alice");
//...}
• The InMemoryAdapter supports – Joins, Transactions, Stored procedures...
![Page 42: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/42.jpg)
44
InMemoryAdapter Configuration
• Tweaking functions– SetKeyColumn– SetAutoIncrementColumn– AddFunction (stored procedure)– ConfigureJoin– ...
![Page 43: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/43.jpg)
45
Design
![Page 44: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/44.jpg)
46
Wrap-Up
![Page 45: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/45.jpg)
47
• OpenSource, Mono• Everything is
dynamic• Fluid-, Indexer Way• CRUD
– FindXXX, DeleteXXX, UpdateXXX etc.
• Dynamics Objects Returned
• Joins, lazy, eager– Natural, WithXXX,
Join
• Various Functions– Group, Order, Scalar,
Modifiers etc.
• Tool & Testing• Design
Wrap-up
![Page 46: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/46.jpg)
48
• Lightweight• Readable• Compelling• Fun to use• Interesing design
• Dynamics extensive testing
• Good understanding upfront
• My Recommendation– Try it and study it– Take it for a spin for some tooling and/or
prototyping– ...and some projects?
Simple.Data in Short
![Page 47: Introduction to Simple.Data](https://reader034.vdocuments.net/reader034/viewer/2022051323/5471fe83b4af9fa90a8b4cff/html5/thumbnails/47.jpg)
49
Further Reading
• Github– https://github.com/markrendle/Simple.Data
• Nuget – http://nuget.org/packages?q=simple.data
• GoogleGroup– https://groups.google.com/forum/?fromgroups#!
forum/simpledata
• Mark Rendle– @MarkRendle– http://blog.markrendle.net/