SQLKata.EntityKata 1.1.1-alpha

This is a prerelease version of SQLKata.EntityKata.
dotnet add package SQLKata.EntityKata --version 1.1.1-alpha
NuGet\Install-Package SQLKata.EntityKata -Version 1.1.1-alpha
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="SQLKata.EntityKata" Version="1.1.1-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SQLKata.EntityKata --version 1.1.1-alpha
#r "nuget: SQLKata.EntityKata, 1.1.1-alpha"
#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 SQLKata.EntityKata as a Cake Addin
#addin nuget:?package=SQLKata.EntityKata&version=1.1.1-alpha&prerelease

// Install SQLKata.EntityKata as a Cake Tool
#tool nuget:?package=SQLKata.EntityKata&version=1.1.1-alpha&prerelease

SQLKata.EntityKata

Entity Modeler using SQLKata

The idea is to focus the code using only entities names instead of SQL queries to model the data, to reduce errors abstract the logic. The library is based on SQLKata and does all the translations under the hood.

Especially useful when entities and database columns have different names. If a property doesn't have the field attribute it will be skipped.

This is the first release for optimization and testing.

Last changes

Added first support for joins. At the current state the project is a playground. I'm trying to find the best syntax for the code, to have a simple and clean code (as much as possible). I'm open to suggestions. Thanks in advance.

Examples

Having the Table:

create table people
(
    PersonId       int auto_increment
        primary key,
    PersonName     varchar(50)  null,
    PersonLastName varchar(50)  null,
    PersonAge      int          null,
    PersonPhone    varchar(50)  null,
    PersonEmail    varchar(100) null,
    constraint People_PersonId_uindex
        unique (PersonId)
);

You can use an entity Person that represents a record in table People

// a separated class form identity can be convenient
[Table("People")]
public class PersonIdentity
{
    [Identity]
    [Field("PersonId")]
    public int Id { get; set; }
}

[Table("People")]
public class Person : PersonIdentity 
{

    [Field("PersonName")]
    public string FirstName { get; set; }
    [Field("PersonLastName")]
    public string LastName { get; set; }
    [Field("PersonAge")]
    public int Age { get; set; }
    [Field("PersonPhone")]
    public string Phone { get; set; }
    [Field("PersonEmail")]
    public string Email { get; set; }
}

var env = new EntityKataService(new MySqlConnection("Server=localhost;Database=entitkatatest;Uid=root;Pwd=;")
    , new MySqlCompiler());

var peopleManager = env.New<Person>();

var people = peopleManager.Get(); // select all 
people = peopleManager.Paginate(1, 10); // select first 10 people

foreach (var person in people)
{
    Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
}

// if you have a separated Id class you can use it, otherwise 
// you can use an anonymous class
// Both of the following are valid    
people = peopleManager.Where(new Person {Id = 2}).Get();
people = peopleManager.Where(new {Id = 2}).Get();
people = peopleManager.Where(Id => 2).Get();
people = peopleManager.Where(new {Id = new GreaterThan(2)}).Get();
people = peopleManager.Where(new {Id = new GreaterThan {Value = 2}}).Get();

people = peopleManager
    .OrderBy(nameof(Person.LastName))
    .Where(new {FirstName = "Gianpiero"})
    .Get();

var insertedRows = peopleManager.Insert(new Person
{
    Age = 33,
    Email = "onon@test.com",
    FirstName = "Marlon",
    LastName = "Bereny",
    Phone = "374873434"
});

Console.WriteLine("Inserted rows: " + insertedRows);

// you can use the same manager for the same type 
// you don't need to create a new one

peopleManager
    .Where(new PersonIdentity {Id = 2})
    .Update(new
    {
        FirstName = "John",
        LastName = "Smith"
    });

if (peopleManager.Where(new {LastName = "Bereny"}).Exists())
{
    Console.WriteLine("Found Bereny");

    var deletedRows = peopleManager.Where(new {FirstName = "Marlon"}).Delete();
    Console.WriteLine("Deleted rows: " + deletedRows);
}

var firstPersonSmith = peopleManager
    .Where(new {LastName = "Smith"})
    .FirstOrDefault();

if (firstPersonSmith != null) Console.WriteLine($"{firstPersonSmith.FirstName} is the first person last name Smith");

people = peopleManager
    .OrderByDesc(nameof(Person.LastName))
    .Get();

// you can also use an object and concatenate different orders
people = peopleManager.Order(new
{
    LastName = Ordering.Ascending,
    FirstName = Ordering.Descending
}).Get();

foreach (var person in people) Console.WriteLine("{0} {1}", person.FirstName, person.LastName);

// First tentative syntax for joins
var p = peopleManager.Join<Order>(Id => PersonId => "=").OrderBy(nameof(Person.LastName)).GetDynamic();

foreach (var person in p)
{
    Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.Notes + " " + person.ItemId);
}

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.1.1-alpha 134 7/24/2022
1.1.0-alpha 108 7/23/2022
1.0.7-alpha 105 7/23/2022
1.0.6-alpha 103 7/23/2022
1.0.5-alpha 104 7/19/2022
1.0.4-alpha 102 7/19/2022
1.0.3-alpha 105 7/19/2022
1.0.2-alpha 96 7/19/2022
1.0.1-alpha 90 7/19/2022
1.0.0-alpha 105 7/18/2022