Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ryankirkman/dapperlite
A Dapper compatible library that works on .NET Compact Framework
https://github.com/ryankirkman/dapperlite
Last synced: about 2 months ago
JSON representation
A Dapper compatible library that works on .NET Compact Framework
- Host: GitHub
- URL: https://github.com/ryankirkman/dapperlite
- Owner: ryankirkman
- Created: 2013-06-05T05:20:13.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-10-22T01:38:06.000Z (about 8 years ago)
- Last Synced: 2023-03-11T17:47:05.186Z (almost 2 years ago)
- Language: C#
- Homepage:
- Size: 558 KB
- Stars: 30
- Watchers: 9
- Forks: 18
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
DapperLite - a very simple object mapper for .NET, .NET Compact Framework and Mono
==================================================================================Features
--------
DapperLite is a [single file](https://github.com/ryankirkman/DapperLite/blob/master/DapperLite.NETCF35/SqlMapper.cs) you can drop in to your .NET Compact Framework or .NET project that will extend your IDbConnection interface.This project is a subset and simplification of dapper-dot-net: https://github.com/SamSaffron/dapper-dot-net
Alternatives
------------
Some other similar projects are:
* [Dapper](https://github.com/SamSaffron/dapper-dot-net)
* [Massive](https://github.com/robconery/massive)
* [PetaPoco](http://www.toptensoftware.com/petapoco/)How to Use
==========Execute a query and map the results to a strongly typed List
------------------------------------------------------------```csharp
public static IEnumerable Query(this IDbConnection cnn, string sql, object param)public static IEnumerable Query(this IDbConnection conn, string sql)
```Example usage:
```csharp
public class Dog
{
public int? Age { get; set; }
public Guid Id { get; set; }
public string Name { get; set; }
public float? Weight { get; set; }
public string Size { get; set; }
}
var guid = Guid.NewGuid();
var dog = connection.Query("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });
dog.Count()
.IsEqualTo(1);dog.First().Age
.IsNull();dog.First().Id
.IsEqualTo(guid);
```Execute a Command that returns no results
-----------------------------------------```csharp
public static int Execute(this IDbConnection cnn, string sql, object param, IDbTransaction transaction)public static int Execute(this IDbConnection cnn, string sql)
```Example usage:
```csharp
connection.Execute(@"
set nocount on
create table #t(i int)
set nocount off
insert #t
select @a a union all select @b
set nocount on
drop table #t", new {a=1, b=2 })
.IsEqualTo(2);
```Using the Micro-ORM Database.cs class
-------------------------------------To use this Micro-ORM, in addition to [SqlMapper.cs](https://github.com/ryankirkman/DapperLite/blob/master/DapperLite.NETCF35/SqlMapper.cs) you will also need to include [SqlMapperInsertUpdate.cs](https://github.com/ryankirkman/DapperLite/blob/master/DapperLite.NETCF35/SqlMapperInsertUpdate.cs), [Database.cs](https://github.com/ryankirkman/DapperLite/blob/master/DapperLite.NETCF35/Database.cs) and [SqlCeDatabase.cs](https://github.com/ryankirkman/DapperLite/blob/master/DapperLite.NETCF35/SqlCeDatabase.cs). These are all extremely simple files and worth reading if you'd like to modify their functionality.
```csharp
// Basic constructor.
protected Database(IDbConnection connection)// Provides advanced configuration for the behaviour of the class when Exceptions are encountered.
protected Database(IDbConnection connection, DapperLiteException exceptionHandler, bool throwExceptions)public T Get(TId id)
public T Get(string columnName, object data)
public IEnumerable All()
public IEnumerable All(string columnName, object data)
public virtual void Insert(object obj)
public virtual void Update(object obj)
// Also includes wrapper methods for Query() and Execute() to save typing.
````Database.cs` is an abstract class designed to be extended with SQL version specific implementations. An `SqlCeDatabase` implementation is provided.
Example usage:
```csharp
SqlCeConnection conn = new SqlCeConnection("Data Source=MyDatabase.sdf");// The type we pass in (Guid) is the type of the Id column that is assumed to be present in every table.
SqlCeDatabase db = new SqlCeDatabase(conn);
// Calling Init() automatically generates a table name map, used to map type names to table names.
// e.g. for the type "Dog", it will first search for a table name == "Dog", then (pluralized) "Dogs"
db.Init();// Get all Dogs.
IEnumerable allDogs = db.All();// Get all Dogs where Size == "big"
IEnumerable bigDogs = db.All("Size", "big");// Get a dog by Id
Dog myDog = db.Get(someGuid);// Insert a new Dog
Dog newDog = new Dog { Age = 3, Id = Guid.New(), Name = "Ralph", Weight = 12.3, Size = "Small" };
db.Insert(newDog);// Get the Dog with Name == "Fido"
Dog yourDog = db.Get("Name", "Fido");// Update a Dog
yourDog.Size = "Small";
db.Update(yourDog);
```