Code Monkey home page Code Monkey logo

dragon-core's Introduction

Dragon Core

Dragon Core is a strong typed ORM tool for .NET 5.

Use the Dragon ORM for .NET framework 2.x-4.x.

What's New

  • V 2.2 Supports .NET 5, C# 9 positional record types
  • V 2.1 Supports .NET 5, C# 9 record types
  • V 2.0 Supports .NET Core 3.1

Quick Start

Install

dotnet add package DragonCore

Define C# Data Types

You can use positional record type from C# 9

public record User (int UserId, string Email);

Or use record type

public record User
{
  public int UserId { get; init; }
  public string Email { get; init; }
}

Or just use regular C# class

public class User
{
  public int UserId { get; set; }
  public string Email { get; set; }
}

Open the database with a connection string

Database database = Database.Open("Server=127.0.0.1;Database=test;User Id=<user-id>;Password=<YourStrong@Passw0rd>;");

Query Database and Create Records/Objects

var users = database.Query<User>("select UserId, Email from UserProfile where UserId=@UserId", new { UserId = 2 }).ToList();

Use the Data

Assert.AreEqual(2, users[0].UserId);
Assert.AreEqual("[email protected]", users[0].Email);

API

The Database class has four methods: Open, Execute, QueryValue, and Query.

  • Open: opens database connection using a connection string.
  • Execute: creates a command and runs ExecuteNonQuery.
  • QueryValue: creates a command and runs ExecuteScalar.
  • Query: creates a command with SQL statement(s) or stored procedure name and creates objects. The Query method supports up to 3 multiple record sets. The result objects are returned in a Tuple, E.g.
Database database = Database.Open(/*connection string*/);
var (users, members, roles) = database.Query<TestUser, TestMemberShip, TestUserRole>(@"
  select * from dbo.UserProfile
  Select * from dbo.webpages_Membership
  select * from dbo.webpages_Roles");

Assert.AreEqual(1, users.ToList()[0].UserId);
Assert.AreEqual(0, members.Count());
Assert.AreEqual("Sysadmin", roles.ToList()[0].RoleName);

Manage Transactions

Use the TransactionScope, guaranteeing that database queries can commit or rollback as a single unit of work.

try
{
    Database database = Database.Open(/*connection string*/);
    using (TransactionScope scope = new TransactionScope())
    {
        database.execute(...);
        database.execute(...);
        database.execute(...);

        // The Complete method commits the transaction. If an exception has been thrown, the transaction is rolled back.
            scope.Complete();
        }
    }
    catch (TransactionAbortedException ex)
    {
        writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
    }
}

Unit Test

The unit test project creates a test database on your SQL server and populates testing data. You can use an existing SQL Server / SQL Server LocalDB or use SQL Server from docker.

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong@Passw0rd>" -p 1433:1433 --name sql -h sql -d mcr.microsoft.com/mssql/server:2019-latest

Once you have identified the SQL server, change the connection string in DragonCore.Tests/DatabaseTest.cs.

static string connectionString = "Server=127.0.0.1;Database=test;User Id=<user-id>;Password=<YourStrong@Passw0rd>;";

And run the unit tests.

dotnet test

Source Code

https://github.com/yysun/dragon-core

Pull Requests are welcome. Have fun coding.

(C) Copyright 2020, Yiyi Sun

dragon-core's People

Contributors

yysun avatar

Stargazers

Brian Redfern avatar Charles Christie avatar Aaron Westre avatar Rasmus Schultz avatar Corentin Girard avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

kaghzi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.