Skip to content

Adds a visual studio C# library project templete (DAL) Data Access Layer, that helps working and connect with sql sever database using app.config file connection string , it also conatins methods that creates and gets the data from data base and save data to data base using data model classes. using Dapper ORM.

License

Notifications You must be signed in to change notification settings

BekoSan/DapperDALProject

Repository files navigation

Dapper DAL Project

this is a visual studio project template , that creates a c# library project with classes and interfaces and methods to work as "Data Access Layer" , using Dapper ORM.

Preview

Dapper New Project

Dapper Library Templete

How To Use?

Part One : Setup Projects

First Setup Your Project

1.Create your project.

2.Open your project app.config file add the connection string of your server.

Dapper Project App Config

Second Setup your Dapper Project

  • Open DataAccess\SqlConnector.cs File and change the db const varibale value to your connection string name in your app.config file.

Dapper Project Sql Connector

Part Two : Wire Up Projects

1.Add the dapper DAL library as reference in your project.

2.Open your program.cs or any file you want to use as connection Initializer in your project, and add this code :

NOTE: First add using statment for the dapper library.

GlobalConfig.InitializeConnection(new SqlConnector());

Part Three : Perform Action on Data Base

Use Create(dataModel,commandText,commandType) method to insert , update , delete in your database, and use Get(commandText,commandType) method to get data from database. those methods are implemented from IDataConnection interface in SqlConnector class.

this is how they look like :

public T Create<T>(T model, string commandText, CommandType commandType = CommandType.StoredProcedure)
        {
            using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.GetConnectionString(db)))
            {

                connection.Execute(commandText, DapperHelper.GenerateParameters(model,false), commandType: commandType);

                return model;
            }
        }

        public List<T> Get<T>(string commandText, CommandType commandType = CommandType.StoredProcedure)
        {
            List<T> output = new List<T>();

            using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.GetConnectionString(db)))
            {

               output =  connection.Query<T>(commandText, commandType: commandType).AsList();

                return output;
            }
        }

To use them , for example you have person model class look like this :

public class PersonModel
{

public int Id {get;set;}

public string FullName {get;set;}

public string Address {get;set;}

}

Say that we have stored procedure called spPerson_Insert and an other one called spPerson_Get, to create new person in your data base you have to do this :

PersonModel p = new PersonModel { Id = 1 , FullName = "Abubakr" , Address = "Sudan" };
GlobalConfig.Connection.Create(p,"spPerson_Insert",CommandType.StoredProcedure);

use the same way with Get() method.

List<PersonModel> people =  GlobalConfig.Connection.Get("spPerson_Get",CommandType.StoredProcedure)

license

MIT license

About

Adds a visual studio C# library project templete (DAL) Data Access Layer, that helps working and connect with sql sever database using app.config file connection string , it also conatins methods that creates and gets the data from data base and save data to data base using data model classes. using Dapper ORM.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages