You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Docs/ormlite/ormlite-overview.md
+303-7Lines changed: 303 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,28 +5,324 @@ for twitter updates.
5
5
ServiceStack.OrmLite is a convention-based, configuration-free lightweight ORM that uses standard POCO classes and Data Annotation attributes to infer its table schema.
6
6
# Introduction
7
7
8
-
OrmLite is a set of light-weight C# extension methods around System.Data.`*` interfaces which is designed to persist POCO classes with a minimal amount of intrusion and configuration.
8
+
OrmLite is a set of light-weight C# extension methods around `System.Data.*` interfaces which is designed to persist POCO classes with a minimal amount of intrusion and configuration.
9
9
Another Orm with similar goals is [sqlite-net](http://code.google.com/p/sqlite-net/).
10
10
11
-
OrmLite was designed with a focus on these core objectives:
11
+
OrmLite was designed with a focus on the core objectives:
12
12
13
-
* Simplicity - with minimal, convention-based attribute configuration
13
+
* Map a POCO class 1:1 to an RDBMS table, cleanly by conventions, without any attributes required.
14
+
* Create/Drop DB Table schemas using nothing but POCO class definitions (IOTW a true code-first ORM)
15
+
* Simplicity - typed, wrist friendly API for common data access patterns.
14
16
* High performance - with support for indexes, text blobs, etc.
17
+
* Amongst the [fastest Micro ORMs](http://servicestack.net/benchmarks/) for .NET (just behind [Dapper](http://code.google.com/p/dapper-dot-net/)).
15
18
* Expressive power and flexibility - with access to IDbCommand and raw SQL
16
19
* Cross platform - supports multiple dbs (currently: Sqlite and Sql Server) running on both .NET and Mono platforms.
17
20
21
+
In OrmLite: **1 Class = 1 Table**. There's no hidden behaviour behind the scenes auto-magically managing hidden references to other tables.
22
+
Any non-scalar properties (i.e. complex types) are text blobbed in a schema-less text field using [.NET's fastest Text Serializer](http://www.servicestack.net/mythz_blog/?p=176).
23
+
Effectively this allows you to create a table from any POCO type and it should persist as expected in a DB Table with columns for each of the classes 1st level public properties.
24
+
25
+
### Other notable Micro ORMs for .NET
26
+
Many performance problems can be mitigated and a lot of use-cases can be simplified without the use of a heavyweight ORM, and their config, mappings and infrastructure.
27
+
As [performance is the most important feature](https://github.com/mythz/ScalingDotNET) we can recommend the following list, each with their own unique special blend of features.
28
+
29
+
***[Dapper](http://code.google.com/p/dapper-dot-net/)** - by [@samsaffron](http://twitter.com/samsaffron) and [@marcgravell](http://twitter.com/marcgravell)
30
+
- The current performance king, supports both POCO and dynamic access, fits in a single class. Put in production to solve [StackOverflow's DB Perf issues](http://samsaffron.com/archive/2011/03/30/How+I+learned+to+stop+worrying+and+write+my+own+ORM). Requires .NET 4.
31
+
***[PetaPoco](http://www.toptensoftware.com/petapoco/)** - by [@toptensoftware](http://twitter.com/toptensoftware)
32
+
- Fast, supports dynamics, expandos and typed POCOs, fits in a single class, runs on .NET 3.5 and Mono. Includes optional T4 templates for POCO table generation.
33
+
***[Massive](https://github.com/robconery/massive)** - by [@robconery](http://twitter.com/robconery)
34
+
- Fast, supports dynamics and expandos, smart use of optional params to provide a wrist-friendly api, fits in a single class. Multiple RDBMS support. Requires .NET 4.
35
+
***[Simple.Data](https://github.com/markrendle/Simple.Data)** - by [@markrendle](http://twitter.com/markrendle)
36
+
- A little slower than above ORMS, most wrist-friendly courtesy of a dynamic API, multiple RDBMS support inc. Mongo DB. Requires .NET 4.
37
+
18
38
# Download
19
-
OrmLite is included with [ServiceStack.zip](https://github.com/downloads/mythz/ServiceStack/ServiceStack.zip) or available to download separately in a standalone
The full source code for OrmLite is also [available online](~/ormlite/ormlite-overview).
40
+
[](http://nuget.org/List/Packages/ServiceStack.OrmLite.SqlServer)
OrmLite is also included in [ServiceStack](https://github.com/ServiceStack/ServiceStack/downloads) or available to download separately in
# Code-first Customer & Order example with complex types on POCO as text blobs
53
+
54
+
Below is a complete stand-alone example. No other config or classes is required for it to run. It's also available as a
55
+
[stand-alone unit test](https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/UseCase/CustomerOrdersUseCase.cs).
56
+
57
+
public enum PhoneType {
58
+
Home,
59
+
Work,
60
+
Mobile,
61
+
}
62
+
63
+
public enum AddressType {
64
+
Home,
65
+
Work,
66
+
Other,
67
+
}
68
+
69
+
public class Address {
70
+
public string Line1 { get; set; }
71
+
public string Line2 { get; set; }
72
+
public string ZipCode { get; set; }
73
+
public string State { get; set; }
74
+
public string City { get; set; }
75
+
public string Country { get; set; }
76
+
}
77
+
78
+
public class Customer {
79
+
public Customer() {
80
+
this.PhoneNumbers = new Dictionary<PhoneType, string>();
81
+
this.Addresses = new Dictionary<AddressType, Address>();
82
+
}
83
+
84
+
[AutoIncrement] // Creates Auto primary key
85
+
public int Id { get; set; }
86
+
87
+
public string FirstName { get; set; }
88
+
public string LastName { get; set; }
89
+
90
+
[Index(Unique = true)] // Creates Unique Index
91
+
public string Email { get; set; }
92
+
93
+
public Dictionary<PhoneType, string> PhoneNumbers { get; private set; } //Blobbed
94
+
public Dictionary<AddressType, Address> Addresses { get; private set; } //Blobbed
Running this against a SQL Server database will yield the results below:
242
+
243
+
[](http://www.servicestack.net/files/ormlite-example.png)
244
+
245
+
Notice the POCO types are stored in the [very fast](http://www.servicestack.net/mythz_blog/?p=176)
246
+
and [Versatile](http://www.servicestack.net/mythz_blog/?p=314)
247
+
[JSV Format](https://github.com/ServiceStack/ServiceStack.Text/wiki/JSV-Format) which although hard to do -
248
+
is actually more compact, human and parser-friendly than JSON :)
249
+
250
+
# API Overview
251
+
252
+
The API is minimal, providing basic shortcuts for the primitive SQL statements:
**Query** statements take in parameterized SQL using properties from the supplied anonymous type (if any)
311
+
312
+
var track3 = dbCmd.Query<Track>("select * from Track Where AlbumName = @album and TrackNo = @trackNo",
313
+
new { album = "Throwing Copper", trackNo = 3 })
314
+
315
+
GetById(s), QueryById(s), etc provide strong-typed convenience methods to fetch by a Table's **Id** primary key field.
316
+
317
+
var track = dbCmd.QueryById<Track>(1);
318
+
23
319
24
320
# Limitations
25
321
26
322
For simplicity, and to be able to have the same POCO class persisted in db4o, memcached, redis or on the filesystem (i.e. providers included in ServiceStack), each model must have an '`Id`' property which is its primary key.
27
323
28
324
29
-
# Examples
325
+
# More Examples
30
326
31
327
In its simplest useage, OrmLite can persist any POCO type without any attributes required:
0 commit comments