Giter VIP home page Giter VIP logo

simplestack.orm's Introduction

SimpleStack.Orm

Build Status Build Status GitHub release (latest by date) GitHub top language Maintenance Twitter URL

SimpleStack.Orm is a layer on top of the wonderful Dapper project that generate SQL queries based on lambda expressions. It is designed to persist types with a minimal amount of intrusion and configuration. All the generated sql queries are using parameters to improve performance and security.

By using Dynamic queries it is also possible to generate queries without a corresponding Type, see Dynamic Queries for more information.

Main goals:

  • Map a Type 1:1 to an RDBMS table or view.
  • Create/Drop DB Table schemas using nothing but a Type. (IOTW a true code-first ORM)
  • Simplicity - typed, wrist friendly API for common data access patterns.
  • Full use of query parameters.
  • Supports multiple databases. Currently: Sql Server, Sqlite, MySql, PostgreSQL)
  • Cross Platform, based on netstandard 2.0.
  • Support connections on multiple databases from the same application

In SimpleStak.Orm : 1 Class = 1 Table/View. There are no surprising or hidden behavior. Attributes may be added on your Type to tune the queries generation (Alias, Schema, PrimaryKey, Index,...)

Sample usage

using SimpleStack.Orm;
using SimpleStack.Orm.SqlServer;

namespace Test{

   public class sample{

      [Alias("dogs")]
      public class Dog{
         [PrimaryKey()]
         [AutoIncrement()]
         public int Id{get; set;}
         public string Name{get; set;}
         [Alias("birth_date")]
         public DateTime? BirthDate{get; set;}
         public decimal Weight{get; set;}
         public string Breed{get; set;}
      }

      var factory = new OrmConnectionFactory(new SqlServerDialectProvider(), "server=...");
      using (var conn = factory.OpenConnection())
      {
         conn.CreateTable<Dog>();

         // INSERT INTO "dogs" ("Id", "Name", "birth_date", "Weight", "Breed" ) VALUES (@p_0, @p_1, @p_2, @p_3, @p_4)
         conn.Insert(new Dog{Name="Snoopy", BirthDate = new DateTime(1950,10,01), Weight=25.4});
         conn.Insert(new Dog{Name="Rex", Weight=45.6});
         conn.Insert(new Dog{Name="Popol", BirthDate = new DateTime(1918,09,13), Weight=2});

         // SELECT "Id", "Name", "birth_date" AS BirthDate, "Weight", "Breed"
         // FROM "dogs"
         // WHERE ("Id" = @p_0)
         // ORDER BY 1 -- ORDER BY is mandatory to use OFFSET and FETCH clause in SQLServer
         // OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
         var rex = conn.First<Dog>(x => Id == 2);

         rex.BirthDate = new DateTime(1994,11,10);

         // UPDATE "dogs" SET "Name"=@p_0, "birth_date"=@p_1, "Weight"=@p_2, "Breed"=@p_3 WHERE "Id"=@p_4
         conn.Update(rex);

         // DELETE FROM "dogs" WHERE ("Name" = @p_0)
         conn.DeleteAll<Dog>(x => x.Name == "Popol");

         // SELECT "Name", "Breed", "Weight"
         // FROM "dogsbackup"
         // WHERE (DATEPART(year,"birth_date") = @p_0) --will be specific depending on database
         // ORDER BY "Breed" ASC,"Weight" DESC
         conn.Select<Dog>(x => {
             x.From("dogsbackup");                         // Change From clause
             x.Select(y => new {y.Name,y.Breed,y.Weight}); // Only return some fields
             x.Where(y => y.BirthDate.Value.Year == 2019);
             x.OrderBy(y => y.Breed)
              .ThenByDescending(y => y.Weight);
         });

         // SELECT AVG(Weight) AS Weight
         // FROM "dogs"
         conn.GetScalar<Dog, decimal>(x => Sql.Avg(x.Weight))
      }
   }
}

simplestack.orm's People

Contributors

andrealbertto avatar avtc avatar bgrainger avatar dependabot[bot] avatar galimage avatar naydfakto avatar schulz3000 avatar tdaron avatar vdaron avatar

Watchers

 avatar

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.