GithubHelp home page GithubHelp logo

marcvangulik / dapper.simplecrud Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ericdc1/dapper.simplecrud

0.0 1.0 0.0 2.63 MB

Who wants to write basic read/insert/update/delete statements? SimpleCRUD provides simple CRUD helpers for Dapper.

License: Other

C# 76.74% Pascal 13.45% Batchfile 0.05% CSS 0.30% ASP 0.06% HTML 3.04% JavaScript 6.36%

dapper.simplecrud's Introduction

Dapper.SimpleCRUD - simple CRUD helpers for Dapper

Features

SimpleCRUD

Dapper.SimpleCRUD is a [single file](https://github.com/ericdc1/Dapper.SimpleCRUD/blob/master/Dapper.SimpleCRUD/SimpleCRUD.cs) you can drop in to your project that will extend your IDbConnection interface. (If you want dynamic support, you need an [additional file](https://github.com/ericdc1/Dapper.SimpleCRUD/blob/master/Dapper.SimpleCRUD%20NET45/SimpleCRUDAsync.cs).)

Who wants to write basic read/insert/update/delete statements?

The existing Dapper extensions did not fit my ideal pattern. I wanted simple CRUD operations with smart defaults without anything extra. I also wanted to have models with additional properties that did not directly map to the database. For example - a FullName property that combines FirstName and LastName in its getter - and not add FullName to the Insert and Update statements.

I wanted the primary key column to be Id in most cases but allow overriding with an attribute.

Finally, I wanted the table name to match the class name by default but allow overriding with an attribute.

This extension adds the following 8 helpers:

  • Get(id) - gets one record based on the primary key
  • GetList<Type>() - gets list of records all records from a table
  • GetList<Type>(anonymous object for where clause) - gets list of all records matching the where options
  • GetList<Type>(string for conditions) - gets list of all records matching the conditions
  • Insert(entity) - Inserts a record and returns the new primary key
  • Update(entity) - Updates a record
  • Delete<Type>(id) - Deletes a record based on primary key
  • Delete(entity) - Deletes a record based on the typed entity

For projects targeting .NET 4.5 or later, the following 8 helpers exist for async operations:

  • GetAsync(id) - gets one record based on the primary key
  • GetListAsync<Type>() - gets list of records all records from a table
  • GetListAsync<Type>(anonymous object for where clause) - gets list of all records matching the where options
  • GetListAsync<Type>(string for conditions) - gets list of all records matching the conditions
  • InsertAsync(entity) - Inserts a record and returns the new primary key
  • UpdateAsync(entity) - Updates a record
  • DeleteAsync<Type>(id) - Deletes a record based on primary key
  • DeleteAsync(entity) - Deletes a record based on the typed entity

If you need something more complex use Dapper's Query or Execute methods!

Note: all extension methods assume the connection is already open, they will fail if the connection is closed.

Install via NuGet - https://nuget.org/packages/Dapper.SimpleCRUD

Check out the model generator T4 template to generate your POCOs. Documentation is at https://github.com/ericdc1/Dapper.SimpleCRUD/wiki/T4-Template

Get a single record mapped to a strongly typed object

 public static T Get<T>(this IDbConnection connection, int id)

Example basic usage:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
      
var user = connection.Get<User>(1);   

Results in executing this SQL

Select Id, Name, Age from [User] where Id = 1 

More complex example:

    [Table("Users")]
    public class User
    {
        [Key]
        public int UserId { get; set; }
        [Column("strFirstName"]
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }
    
    var user = connection.Get<User>(1);  

Results in executing this SQL

Select UserId, strFirstName as FirstName, LastName, Age from [Users] where UserId = @UserID

Notes:

  • The [Key] attribute can be used from the Dapper namespace or from System.ComponentModel.DataAnnotations

  • The [Table] attribute can be used from the Dapper namespace, System.ComponentModel.DataAnnotations.Schema, or System.Data.Linq.Mapping - By default the database table name will match the model name but it can be overridden with this.

  • The [Column] attribute can be used from the Dapper namespace, System.ComponentModel.DataAnnotations.Schema, or System.Data.Linq.Mapping - By default the column name will match the property name but it can be overridden with this. You can even use the model property names in the where clause anonymous object and SimpleCRUD will generate a proper where clause to match the database based on the column attribute

  • GUID (uniqueidentifier) primary keys are supported (autopopulates if no value is passed in)

Execute a query and map the results to a strongly typed List

public static IEnumerable<T> GetList<T>(this IDbConnection connection)

Example usage:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
var user = connection.GetList<User>();  

Results in

Select * from [User]

Execute a query with where conditions and map the results to a strongly typed List

public static IEnumerable<T> GetList<T>(this IDbConnection connection, object whereConditions)

Example usage:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
  
var user = connection.GetList<User>(new { Age = 10 });  

Results in

Select * from [User] where Age = @Age

Notes:

  • To get all records use an empty anonymous object - new{}
  • The where options are mapped as "where [name] = [value]"
  • If you need > < like, etc simply use the manual where clause method or Dapper's Query method

Execute a query with a where clause and map the results to a strongly typed List

public static IEnumerable<T> GetList<T>(this IDbConnection connection, string conditions)

Example usage:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
  
var user = connection.GetList<User>("where age = 10 or Name like '%Smith%'");  

Results in

Select * from [User] where age = 10 or Name like '%Smith%'

Notes:

  • This uses your raw SQL so be careful to not create SQL injection holes
  • There is nothing stopping you from adding an order by clause using this method

Insert a record

public static int Insert(this IDbConnection connection, object entityToInsert)

Example usage:

[Table("Users")]
public class User
{
   [Key]
   public int UserId { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int Age { get; set; }

   //Additional properties not in database
   [Editable(false)]
   public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
   public List<User> Friends { get; set; }
   [ReadOnly(true)]
   public DateTime CreatedDate { get; set; }
}

var newId = connection.Insert(new User { Name = "User", Age = 10 });  

Results in executing this SQL

Insert into [Users] (FirstName, LastName, Age) VALUES (@FirstName, @LastName, @Age)

Notes:

  • Default table name would match the class name - The Table attribute overrides this
  • Default primary key would be Id - The Key attribute overrides this
  • By default the insert statement would include all properties in the class - The Editable(false) and ReadOnly(true) attributes remove items from the insert statement
  • Properties decorated with ReadOnly(true) are only used for selects
  • Complex types are not included in the insert statement - This keeps the List out of the insert even without the Editable attribute. You can include complex types if you decorate them with Editable(true). This is useful for enumerators.

Update a record

public static int Update(this IDbConnection connection, object entityToUpdate)

Example usage:

[Table("Users")]
public class User
{
   [Key]
   public int UserId { get; set; }
   [Column("strFirstName")]
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int Age { get; set; }

   //Additional properties not in database
   [Editable(false)]
   public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
   public List<User> Friends { get; set; }
}
connection.Update(entity);

Results in executing this SQL

Update [Users] Set (strFirstName=@FirstName, LastName=@LastName, Age=@Age) Where ID = @ID

Delete a record

public static int Delete<T>(this IDbConnection connection, int Id)

Example usage:

public class User
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int Age { get; set; }
}
connection.Delete<User>(newid);

Or

public static int Delete<T>(this IDbConnection connection, T entityToDelete)

Example usage:

public class User
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int Age { get; set; }
}

connection.Delete(entity);

Results in executing this SQL

Delete From [User] Where ID = @ID

Database support

  • There is an option to change database dialect. Default is Microsoft SQL Server but can be changed to PostgreSQL or SQLite and possibly others down the road.
   SimpleCRUD.SetDialect(SimpleCRUD.Dialect.PostgreSQL);
   
   SimpleCRUD.SetDialect(SimpleCRUD.Dialect.SQLite);

Do you have a comprehensive list of examples?

Dapper.SimpleCRUD has a basic test suite in the test project

There is also a sample website showing working examples of the the core functionality in the demo website

Future

I am considering the following based on feedback:

  • Count methods
  • Support for more database types (Firebird, SQLCe, etc)
  • Add paged getlist method for paging long lists

dapper.simplecrud's People

Contributors

ericdc1 avatar psantiago avatar jdehlin avatar yogiraja avatar tim-lukacik avatar izhar-ap avatar michelc 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.