Git Product home page Git Product logo

dappir's Introduction

Dappir - more extensions for dapper

nuget

PM> Install-Package Dappir

Features

This is a small tribute to our developer department DAPI; Why the name Dappir?: Dapper + Dapi = Dappir. Departamento de Aprimoramento da Primeira Instância - DAPI

Dappir contains a number of helper methods for inserting, getting, updating and deleting records and on cascade too \o/.

You remember of Contrib, ow yeah, its true, but is not, haha!!!

This is for SQL SERVER still, but we want them for all data bases. do you want help us, do fork now.

The full list of extension methods in Dappir on 'IDbTransaction' right now are:

//we are using this litle interface for facibily, all methods have this interface like constraint
public interface IModel { }

//now we have this methods

//inserts
void Insert<TModel>(TModel entity);
void InsertAll<TModel>(IEnumerable<TModel> listEntity);
void InsertOnCascade<TModel>(TModel entity);

//selects
IEnumerable<TModel> SelectAll<TModel>();
IEnumerable<TModel> Select<TModel>(object filterDynamic);
TModel Select<TModel>(int key);
TModel SelectOnCascade<TModel>(int key);

//updates
void Update<TModel>(TModel entity);
void UpdateAll<TModel>(IEnumerable<TModel> listEntity);
void UpdateOnCascade<TModel>(TModel entity);

//deletes
void Delete<TModel>(int key);
void Delete<TModel>(TModel entity);
void DeleteAll<TModel>(IEnumerable<TModel> listEntity);
void DeleteOnCascade<TModel>(int key);

For these extensions to work, the entity in question MUST have a key property decorate with [Column(IsPrimaryKey = true)].

public class Car : IModel
{
    [Column(IsPrimaryKey = true)]
    public int CarId { get; set; }
    public string Name { get; set; }
}

For your entity working with cascade, you must decorate yours property on [Association].

public class Car : IModel
{
    [Column(IsPrimaryKey = true)]
    public int CarId { get; set; }
    public string Name { get; set; }
    
    [Association]
    public Carmaker Maker { get; set; }

    [Association]
    public List<Dealership> Dealerships { get; set; }
}

public class Carmaker : IModel
{
    [Column(IsPrimaryKey = true)]
    public int CarmakerId { get; set; }
    public int CarId { get; set; }
    public string Name { get; set; }
}

public class Dealership : IModel
{
    [Column(IsPrimaryKey = true)]
    public int DealershipsId { get; set; }
    public int CarId { get; set; }
    public string Name { get; set; }
}

CarId Look this is the relationship between entities.

Select methods

Get one specific entity based on id

var car = transaction.Select<Car>(1);

var carmaker = transaction.Select<Carmaker>(1);

var listDealerships = transaction.Select<Dealership>(new { CarId = 1 });

or a list of all entities in the table.

var listDealerships = transaction.SelectAll<DealershipCar>();

or still you can select on cascade, look that:

var car = transaction.SelectOnCascade<Car>(1);

var carmaker = car.Maker;

var listDealerships = car.Dealerships;

\o/ its amazing Yeah, cascade is very nice.

Insert methods

Insert one entity

var car = new Car { Name = "520" };
car.Maker = new Carmaker { "Volvo" };
car.Dealerships = new List<Dealership> 
{ 
    new Dealership { Name = "Veronica Vehicles" }, 
    new Dealership { Name = "Heavy Loader Trucks" } 
};

transaction.Insert(car);

car.Maker.CarId = car.CarId;

transaction.Insert(car.Maker);

car.Dealerships.ForEach(x =>
{
    x.CarId = car.CarId;
    transaction.Insert(x);
})

or a list of entities.

var car = new Car { Name = "520" };
car.Maker = new Carmaker { "Volvo" };
car.Dealerships = new List<Dealership> 
{ 
    new Dealership { Name = "Veronica Vehicles" }, 
    new Dealership { Name = "Heavy Loader Trucks" } 
};

transaction.Insert(car);

car.Maker.CarId = car.CarId;

transaction.Insert(car.Maker);

car.Dealerships.ForEach(x => x.CarId = car.CarId);

transaction.InsertAll(car.Dealerships);

or insert with cascade, more easy:

var car = new Car { Name = "520" };
car.Maker = new Carmaker { "Volvo" };
car.Dealerships = new List<Dealership> 
{ 
    new Dealership { Name = "Veronica Vehicles" }, 
    new Dealership { Name = "Heavy Loader Trucks" } 
};

transaction.InsertOnCascade(car);

Update methods

Update one specific entity

transaction.Update(new Car() { CarId = 1, Name = "Saab" });

or update a list of entities.

transaction.UpdateAll(cars);

and ofcourse, cascade.

transaction.UpdateOnCascade(car);

Delete methods

Delete an entity by the specified [Column(IsPrimaryKey = true)] property

transaction.Delete(new Car() { CarId = 1 });
transaction.Delete<Car>(1);

a list of entities

transaction.DeleteAll(cars);

and our good and friend old cascade

pay attention to the external relations with the car class on use of cascade.

transaction.DeleteOnCascade<Car>(1);

Special Attributes

Dappir makes use of some optional attributes:

  • [Table("Tablename")] - use another table name instead of the name of the class

    [Table ("emps")]
    public class Employee : IModel
    {
        [Column(IsPrimaryKey = true)]
        public int EmployeeId { get; set; }
        public string Name { get; set; }
    }

if dependencies do not resolve correctly, copy and paste the DappirHelperExtensions class into your project

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.