DotNetHelper.Database

DotNetHelper.Database is a lightweight ORM that works with generics,dynamic, & anonyous objects for ADO.NET


Keywords
ORM, database, sql, generic, dynamic, anonyous
License
MIT
Install
Install-Package DotNetHelper.Database -Version 1.1.6

Documentation

DotNetHelper.Database

*DotNetHelper.Database is a ORM that support CRUD + upsert operations with any generic,dynamic, & anonyous objects for .NET *

|| Documentation • API • Tutorials || Change Log • || View on Github||

Package Tests Code Coverage
Build Status Build Status codecov
Continous Integration Windows Linux MacOS
AppVeyor Build status
Azure Devops Build Status Build Status Build Status

Features

  • Can dynamically build your sql statement from any (Generic,Anonymous,Dynamic) object but also support you providing one as well
  • Can (Insert, Update, Upsert, Delete) any (Generic,Anonymous,Dynamic) object into a database
  • Support Any DbConnection
  • Support auto-serializing & deserializing columns that is stored as CSV,JSON, & XML format in the database
    • You implement the serialization so we don't have to depend on libraries like newtonsoft.json
  • Map List To DataTable
  • Map DataTable To List
  • Map IDataReader To List
  • Map DataRow To A Class
  • Insert with return Identity

Supports

  • SqlServer
  • Sqlite
  • MySql
  • Oracle

Getting Started

insert,update,upsert,delete using a anonymous object
using (var db = new SqlConnection().DB()) 
{
      // Same api can be used for Update,Upsert,Delete by changing the ActionType enum 
      db.Execute(new {Id = 1, Name = "John Doe"}, ActionType.Insert,"TableName"); 
}
insert,update,upsert,delete using a dynamic object
dynamic dyn = new ExpandoObject(); 
        dyn.Id = 2;
        dyn.Name = "John Sister";
using (var db = new SqlConnection().DB()) 
{
   // Same api can be used for Update,Upsert,Delete by changing the ActionType enum
      db.Execute(dyn, ActionType.Insert,"TableName"); 
}
insert,update,upsert,delete using a generic class
using (var db = new SqlConnection().DB()) 
{
      var employee = new Employee(){ Id = 1, Name = "Generic Name" }  
   // Same api can be used for Update,Upsert,Delete by changing the ActionType enum
      db.Execute(employee, ActionType.Insert); 
   // table name not required if Table Attribute exist otherwise type name would be used as table name
}

Insert record and return database generated values such as Identity & more

Lets create a table
CREATE TABLE [Employee](
	[Id] [int] NOT NULL IDENTITY (1,1) PRIMARY KEY,
	[Name] [varchar](400) NOT NULL,
	[CreatedAt] DateTime NOT NULL DEFAULT GETDATE()
);
Lets create our DTO from our table
public class Employee {
  
  [SqlColumn(SetIsIdentityKey = true)] // Specify that this column is an Identity field
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  // or you can use data annotation attribute for same behavior
  public int Id { get; set; }

  public string Name { get; set; }
  
  [SqlColumn(SetIsReadOnly = true)] // If true this property will never be included when creating insert sql. This is useful for senarios where you want to use the database default value
  [NotMapped] // or you can use data annotation attribute for same behavior
  public DateTime CreatedAt { get; set; }

}
now lets insert an employee and get back all the database generated values such as CreatedAt & Identity Id
using (var db = new SqlConnection().DB()) 
{
  var employee = new Employee() {Name = "Generic Name"};
  var outputEmployee = await db.ExecuteAndGetOutputAsync(employee, ActionType.Insert,emp => emp.Id, emp => emp.CreatedAt);
  // Set the identity and database default value back to my original unchanged object
  employee.Id = outputEmployee.Id;
  employee.CreatedAt = outputEmployee.CreatedAt;

  Console.WriteLine(employee.Id); // 1
  Console.WriteLine(employee.CreatedAt); // 7/24/2020 7:55:45 PM
  Console.WriteLine(employee.Name); // Generic Name
}

Documentation

For more information, please refer to the Officials Docs

Solution Template

badge