Skip to content

raphaelcrejo/DynamicDAO

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DynamicDAO - Providing an easy way to access databases and fill objects

license .NET Donate

Note: This repository contains the .NET Standard 2.1 implementation of the library. For the .NET Framework implementation, click here

About

DynamicDAO is a .NET MicroORM that allows you to access and work with your database within minimal efforts.

Features

  • Creates an IDBConnection based on pre-defined information (Database provider, connection string, parameter identifier and database command type)
  • Execute methods based on mapped objects or manually entered parameters
  • Queries are executed and returning data are converted to object automatically

A little code

Setting up your provider information

ProviderInfo provider = new ProviderInfo("Data Source=.\SQLEXPRESS;Initial Catalog=tempdb;User ID=sa;Password=adm")
// Defaut parameters: 
// - providerName: System.Data.SqlClient
// - identifier  = @
// - commandType = CommandType.StoredProcedure
// - commandTimeout = 30sec
// - lockTransaction = false
// - isolationLevel = IsolationLevel.ReadCommited (works only when lockTransaction is true)

Creating a data access object

using DynamicDAO;
...
using (AutoDAO db = new AutoDAO(provider))
{
    // Your code here
}

or

using DynamicDAO;
...
// Assumes default settings, like ProviderInfo
using (AutoDAO db = new AutoDAO("Data Source=.\SQLEXPRESS;Initial Catalog=tempdb;User ID=sa;Password=adm"))
{
    // Your code here
}

Adding manual parameters to the data access object

Considers the following procedure:

-- @PERSON_NAME VARCHAR(100)
-- @PERSON_AGE INT
-- @PERSON_DOB DATE

INSERT INTO dbo.Person
    (PersonName, PersonAge, PersonDOB)
VALUES
    (@PERSON_NAME, @PERSON_AGE, @PERSON_DOB)
using (AutoDAO db = new AutoDAO(provider))
{
    objetc[][] inputParameters = new object[3][];

    inputParameters[0] = new object[] { "NAME", "Raphael Crejo" };
    inputParameters[1] = new object[] { "AGE", 37 };
    inputParameters[2] = new object[] { "DOB", new DateTime(1985, 3, 15) };
    
    db.AddInputParameters(inputParameters);
    // Your code here
}

Adding mapped object to data access object

Considers the following class:

using System.Data;
using DynamicDAO.Mapping;
...
public class Person
{
    [Field("PersonId", "PERSON_ID")]
    public long PersonId { get; set; }
    
    [Field("PersonName", "PERSON_NAME")]
    [Parameter("PersonName", "PERSON_NAME", ParameterDirection.Input)]
    public string PersonName { get; set; }
    
    [Field("PersonAge", "PERSON_AGE")]
    [Parameter("PersonName", "PERSON_AGE", ParameterDirection.Input)]
    public int PersonAge { get; set; }
    
    [Field("PersonDOB", "PERSON_DOB")]
    [Parameter("PersonName", "PERSON_DOB", ParameterDirection.Input)]
    public DateTime PersonDOB { get; set; }
}
Person person = new Person
{
    Name = "Raphael da Cunha Crejo",
    Age = 39,
    DOB = new DateTime(1985, 3, 15)
};

using (AutoDAO db = new AutoDAO(provider))
{
    db.AddParameters(person);
    // Your code here
}

ExecuteScalar and ExecuteNonQuery methods

object result = db.ExecuteScalar("INSERT_PERSON"); // considering that your stored procedure returns the Person ID
int result = db.ExecuteNonQuery("INSERT_PERSON");
// or just
db.ExecuteNonQuery("INSERT_PERSON");

Query<T> and QueryList<T> methods

Considers the Person class above:

Person person = db.Query<Person>("SELECT_PERSON");
List<Person> personList = db.QueryList<Person>("SELECT_PERSONS");

Removing parameters

db.ClearParameters(); // remove all parameters from IDBCommand
db.RemoveParameters(new string[] { "PERSON_DOB" }); // remove specific parameter from IDBCommand

About

Providing an easy way to access databases and fill objects

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages