DapperGlib 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package DapperGlib --version 1.0.1
NuGet\Install-Package DapperGlib -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DapperGlib" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DapperGlib --version 1.0.1
#r "nuget: DapperGlib, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DapperGlib as a Cake Addin
#addin nuget:?package=DapperGlib&version=1.0.1

// Install DapperGlib as a Cake Tool
#tool nuget:?package=DapperGlib&version=1.0.1

This is a beta phase of the project, it is still under construction!!

Dapper.DapperGlib

Introduction

This Dapper extension will allow you to map the database in an easy way, inspired by Laravel Eloquent. Has Relationships, Query Builder, Basic Where Clauses, Advanced Where Clauses, Ordering and more. If you have any questions, suggestions or bugs, please don't hesitate to contact me or create an issue.


Download

Install-Package DapperGlib

Configuration

in the appsettings.json file placed in the root folder of your startup project you must define the connection string

{
  "ConnectionStrings": {
    "SqlConnection": "Your connection string"
  }
}

Getting Started

#Model Conventions

As I mentioned before DapperGlib is inspired by Eloquent Laravel, so we will be working with Models. let's examine a basic model class and explain each part.

using DapperGlib;

namespace App.Models
{
    public class Actor : Model<Actor>
    {
        public Actor()
        {
        }
    }
}

#Table Names

After looking at the example above, you may have noticed that we didn't specify which database table corresponds to our Actor Model. This is because the name of the Class will be used as the name of the table unless another name is explicitly specified. So in this case it will be assumed that the Actor model stores records in the Actor table

If your model's corresponding database table does not fit this convention, you may manually specify the model's table name by defining a table property on the model:

public class Actor : Model<Actor>
{
    [TableName]
    public override string? Table { get; } = "Actors";
    ...
}

Note that the TableName attribute must be used on the Property

#Primary Keys

Unlike the table name, the primary key property must always be specified. The primary key will be used for action queries like Update, Delete, Etc. We need to use the PrimaryKey Attribute on the Property.

public class Actor : Model<Actor>
{
    [PrimaryKey]
    public int ActorId { get; set; }
    ...
}

In addition, We assumes that the primary key is an incrementing integer value

#Fillable Properties

Properties that are to be filled and are defined in the Model table must be specified with the Fillable attribute.

public class Actor : Model<Actor>
{
    [Fillable]
    public string Name { get; set; }
    ...
}

#Inserting & Updating Models

Once you have created a model and its associated database table, you are ready to start interacting with data from your database. You can think of each Model as a query builder allowing you to fluently query the database table associated with the model.

#Insert

Now insert records will be very simple.
To insert a new record into the database, you should instantiate a new Model instance and set attributes on the Model. Then, call the Create static method on the Model instance:

Actor ActorToSave = new();

ActorToSave.Name = "Keanu";
ActorToSave.LastName = "Reeves";

Actor NewActor = Actor.Create(ActorToSave);

You can keep it simpler by using:

Actor NewActor = Actor.Create(new()
{
    Name = "Keanu",
    LastName = "Reeves"
});
#Update

You can use the Update function to update an existing Model in the database.
The Update function will create a query based on ALL Fillable Properties and the Primary Key and update the record in the database.
Following the example above:


NewActor.Name = "Tom";
NewActor.LastName = "Hanks";
NewActor.Update(); 

If you want to update only specific properties you can pass an anonymous object to the Update function

NewActor.Update(new
{
    LastName = "Holland",
});

#Retrieving Models

Of course you can get data from your database using very simple functions.
The model's ToList method will retrieve all of the records from the model's associated database table:


List<Actor> Actors = Actor.ToList();

and like this, the Model has a list of functions that will allow you to retrieve data from the database quickly

  • First
  • FirstOrDefault
  • FirstAsync
  • FirstOrDefaultAsync
  • Find
  • FindAsync
  • FindOrDefault
  • FindOrDefaultAsync
  • ToList
  • ToListAsync

#Deleting Models

You can delete a record using the Delete function

NewActor.Delete();

Query Builder

Query Builder provides a convenient fluent interface for creating and executing database queries. It can be used to perform most database operations.

#Basic Where Clauses

#Where Clauses

You may use the query builder's where method to add "where" clauses to the query. The most basic call to the where method requires three arguments. The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported operators. The third argument is the value to compare against the column's value.

var actor = Actor.Where("Name", "=", "Tom").First();

If you want to verify that a column is = to a given value, you may pass the value as the second argument to the where method.
The Query Builder will assume you would like to use the = operator:

var actor = Actor.Where("Name", "Tom").First();

As previously mentioned, you may use any operator that is supported by your database system:

var actor = Actor.Where("Name", "like", "%Tom%").First();

var actor = Actor.Where("Votes", ">=", 80).First();
#Or Where Clauses

When chaining together calls to the query builder's where method, the "where" clauses will be joined together using the and operator.
However, you may use the orWhere method to join a clause to the query using the or operator. The orWhere method accepts the same arguments as the where method:

var actor = Actor.Where("Name","Tom").OrWhere("Name", "Keanu").First();
#Additional Where Clauses

WhereBetween / WhereDateBetween

The WhereBetween method verifies that a column's value is between two values.
Receives two parameters, the first is the column's name and the second is an instance of the DapperGlib.Util.Between class which only has From and To properties

List<Actor> Actors = Actor.WhereBetween("Votes", new()
{
    From = 50,
    To = 100
}).ToList();
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.4.2 348 1/2/2023
1.3.4.1 292 12/7/2022
1.3.4 291 12/7/2022
1.3.3 394 10/5/2022
1.3.2 413 9/27/2022
1.3.1 389 8/30/2022
1.3.0 432 8/7/2022
1.2.6 419 8/4/2022
1.2.5 426 8/2/2022
1.2.4 449 8/1/2022
1.2.3 417 7/28/2022
1.2.2 414 7/27/2022
1.0.1 423 7/18/2022
1.0.0 457 7/16/2022