RepoDb edit

A lightweight Object Relational Mapper (ORM) library for .Net applications, who claimed to be the fastest ORM for .Net as of today. It is inspired and developed from Dapper ORM .Net library. Founded and developed by Michael Pendon and published as an open source library via Nuget Package.

Development edit

The library development was started April 2018 when Michael has encountered some difficulties of their design of the ORM patterns (and repositories) on their department. It is for the reason in order to simplify and speed-up the development process, and to adapt immediately to the on-demand request of the businesses (requirements).

Michael's purpose is to simplify the implementations of the Data Access models on the .Net application side and by making sure that the right pattern and cleanliness of the codes are implemented.

He is very keen on making the library more flexible than any other ORM existing on the community. He is saying that "Fast-switching development approach between the massive and lightweight ORM operations are very important on solving the complex problems. ORM alone could not solve the problem, unless you are enabling the fast-switching feature when the situation is needed to be implemented during the development phase".

Features edit

RepoDb feature lives between big ORMs (of like Entity Framework and NHibernate) and mini-ORMs (of like Dapper and Massive). The main purpose is to have a fast-switching approach in the middle of the development.

Below are the list of the features.

  • Operations (Asynchronous)
  • Type Mapping
  • Field Mapping
  • Multiple Mapping
  • Expression Tree
  • Recursive Query (very fast)
  • Caching (default RepoDb.MemoryCache)
  • Tracing
  • SQL Statement Builder
  • Transactions

Expression Tree edit

The expression tree is the brain of the library. It defines the best possible way of doing a WHERE expression (SQL Statement) by composing it via dynamic or QueryGroup objects.

An explicit query expression are using the defined objects RepoDb.QueryGroup, RepoDb.QueryField, RepoDb.Enumerations.Conjunction and RepoDb.Enumerations.Operation when composing the expression.

Sample explicit Expression Tree.

    var query = new QueryGroup
    (
         new []
         {
              new QueryField("CustomerId", Operation.GreaterThanOrEqual, 10045),
              new QueryField("CreatedDate", Operation.GreaterThanOrEqual, DateTime.UtcNow.Date.AddMonths(-3))
         },
         null, // Child QueryGroups
         Conjunction.And
    );

A dynamic query expression is using a single dynamic object when composing the expression.

Sample dynamic Expression Tree.

    var query = new
    {
         CustomerId = new { Operation = 10045 },
         CreatedDate = new { Operation = Operation.GreaterThanOrEqual, DateTime.UtcNow.Date.AddMonths(-3) }
    };

Code Samples edit

The is very catchy and is very clean. All repository operations can all be called via IDbConnection extended method. See sample codes below using the repository.

BatchQuery:

    using (var repository = new DbRepository<SqlConnection>(@"Server=.;Database=Northwind;Integrated Security=SSPI;"))
    {
         repository.BatchQuery<Order>(new { CustomerId = 10045 }, 0, 24);
    }

Count:

    using (var repository = new DbRepository<SqlConnection>(@"Server=.;Database=Northwind;Integrated Security=SSPI;"))
    {
         var rows = repository.Count<Customer>();
    }

Delete:

    using (var repository = new DbRepository<SqlConnection>(@"Server=.;Database=Northwind;Integrated Security=SSPI;"))
    {
         var affectedRows = repository.Delete<Customer>(251);
    }

ExecuteNonQuery:

    using (var repository = new DbRepository<SqlConnection>(@"Server=.;Database=Northwind;Integrated Security=SSPI;"))
    {
         var commandText = @"UPDATE [dbo].[Order] SET Quantity = @Quantity, UpdatedDate = @UpdatedDate WHERE (CustomerId = @CustomerId);";
         var result = repository.ExecuteNonQuery(commandText, new
         {
               CustomerId = 10045,
               Quantity = 5,
               UpdatedDate = DateTime.UtcNow
         });
    }

ExecuteQuery:

    using (var repository = new DbRepository<SqlConnection>(@"Server=.;Database=Northwind;Integrated Security=SSPI;"))
    {
         var result = repository.ExecuteQuery<Order>("SELECT * FROM [dbo].[Order] WHERE CustomerId = @CustomerId;", new
         {
               CustomerId = 10045
         });
    }

ExecuteScalar:

    using (var repository = new DbRepository<SqlConnection>(@"Server=.;Database=Northwind;Integrated Security=SSPI;"))
    {
         var id = repository.ExecuteScalar("SELECT MAX([Id]) AS MaxId FROM [dbo].[Order] WHERE CustomerId = @CustomerId;", new
         {
               CustomerId = 10045
         });
    }

Insert:

    using (var repository = new DbRepository<SqlConnection>(@"Server=.;Database=Northwind;Integrated Security=SSPI;"))
    {
         var order = new Order()
         {
               CustomerId = 10045,
               ProductId = 12
               Quantity = 2,
               CreatedDate = DateTime.UtcNow
         };
         repository.Insert(order);
    }

Merge:

    using (var repository = new DbRepository<SqlConnection>(@"Server=.;Database=Northwind;Integrated Security=SSPI;"))
    {
         var order = repository.Query<Order>(1);
         order.Quantity = 5;
         UpdatedDate = DateTime.UtcNow;
         repository.Merge(order, Field.Parse(new { order.Id }));
    }

Truncate:

    using (var connection = new SqlConnection(@"Server=.;Database=Northwind;Integrated Security=SSPI;").EnsureOpen())
    {
         connection.Truncate<Customer>();
    }

Executing a StoredProcedure:

    using (var connection = new SqlConnection(@"Server=.;Database=Northwind;Integrated Security=SSPI;").EnsureOpen())
    {
         using (var reader = connection.ExecuteReader("[dbo].[sp_GetCustomer]", new { Id = 10045 }, commandType: CommandType.StoredProcedure))
         {
               while (reader.Read())
               {
                       // Process each row here
               }
          }
     }

Not all operations are shown. Please visit the documentation for complete samples.

Release edit

The first release of library will be on version 1.2.0, in between mid or last week of July 2018.

Links edit

Documentation: https://repodb.readthedocs.io/en/latest/index.html

Package: https://www.nuget.org/packages/RepoDb/

Project: https://github.com/mikependon/RepoDb