EasyDb

EasyDb is a Micro Orm that allows you to write less code.

Download EasyDb from CodePlex or install using NuGet




You can use « EntityGen », a tool that generates entities and Repositories for EasyDb.

Connection

  • You can use default connection ("defaultConnection" specified in App.config file)
IDb db = new Db();
  • Or you can specify your connectionString name if you have several connectionStrings (in App.config)
IDb db = new Db("myConfigConnectionString");
  • Also, you can specify the connectionString and providerName in code.
IDb db = new Db(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;", "System.Data.SqlClient");

Read All

IDb db = new Db();
var result = db.ReadAllMapTo<Customer>("select * from [dbo].[Customers]");
  • Read All Async
IDb db = new Db();
var result = await db.ReadAllMapToAsync<Customer>("select * from [dbo].[Customers]");
  • With Stored Procedure
IDb db = new Db();
var result= db.ReadAllMapTo<Customer>(CommandType.StoredProcedure, "sp_getcustomers");
  • You can also use your method to create an instance with the DbDataReader
IDb db = new Db();
var result = await db.ReadAllAsync<Person>("Select * from [dbo].[Person]",
                (reader) =>
                {
                    var person = new Person();
                    person.PersonID = (int)reader["PersonID"];
                    person.FirstName = (string)reader["FirstName"];
                    person.LastName = (string)reader["LastName"];
                    person.Email = reader["Email"] == DBNull.Value ? default(string) : (string)reader["Email"];

                    return person;
                });

Read One

  • Create a parameter
var parameter = new EasyParam("@id",10);

 IDb db = new Db();
 var person = db.ReadOneMapTo<Person>("Select * from [dbo].[Person] where PersonID =@id", new EasyParam("@id", 1));
  • Read One Async
IDb db = new Db();
var person = await db.ReadOneMapToAsync<Person>("Select * from [dbo].[Person] where PersonID =@id", new EasyParam("@id", 1));

ExecuteScalar

IDb db = new Db();
int result = db.Scalar("select count(*) from dbo.Person");
  • ExecuteScalar Async
IDb db = new Db();
 int result = await db.ScalarAsync("select count(*) from dbo.Person");

ExecuteNonQuery

IDb db = new Db();
string query = "insert into dbo.Person(FirstName,LastName,Email)values(@firstName,@lastName,@email)";
var parameters = new EasyParam[]{
    new EasyParam("@firstName","Marie"),
    new EasyParam("@lastName","Bellin"),
    new EasyParam("@email","mb123@live.com")
};
var result = db.NonQuery(query, parameters);
  • ExecuteNonQuery Async
IDb db = new Db();
string query = "insert into dbo.Person(FirstName,LastName,Email)values(@firstName,@lastName,@email)";
var parameters = new EasyParam[]{
    new EasyParam("@firstName","Marie"),
    new EasyParam("@lastName","Bellin"),
    new EasyParam("@email","mb123@live.com")
};
var result = await db.NonQueryAsync(query, parameters);

Transactions

IDb db = new Db("Northwind");
try
{
    using (var scope = new TransactionScope())
    {
       var categoryID = await db.ScalarAsync("insert into dbo.Categories(CategoryName,Description)values(@categoryName,@description); select @@identity;",
           new EasyParam[]{
               new EasyParam("@categoryName","Fruits"),
               new EasyParam("@description","Delicious fruits.")
             });

       var result = await db.NonQueryAsync("insert into dbo.Products([ProductName],[CategoryID],[UnitPrice]) values(@productName,@categoryID,@unitPrice)",
           new EasyParam[]{
                new EasyParam("@productName","Golden"),
                new EasyParam("@categoryID",categoryID),
                new EasyParam("@unitPrice","0.50")
             });

       scope.Complete();
    }
}
catch (Exception) { }

Blog

Last edited Oct 15, 2014 at 9:50 PM by romagny13, version 40