Dapper Data Access Layer (Extension for Visual Studio)

Introduction

 
This extension adds a Visual Studio C# library project template (DAL) Data Access Layer that helps with working and connecting to the SQL server database using an app.config file connection string. It also contains methods that create and gets the data from the database and saves data to the database using data model classes and Dapper ORM. 
 
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.
 
v1.0
 
Support for Visual Studio 2017.
 
V1.1 
 
Support for Visual Studio 2019.
 
Preview
 
Dapper Data Access Layer (Extension For Visual Studio)
 
Dapper Data Access Layer (Extension For Visual Studio)

Part One - Setup Projects

 
First, set up your project
  1. Create your project.
  2. Open your project app.config file add the connection string of your server.
Next, set up 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.
 

Part Two - Wire Up Projects

  1. Add the dapper DAL library as a 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 a using statement for the dapper library.
  1. 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:
  1. public T Create<T>(T model, string commandText, CommandType commandType = CommandType.StoredProcedure)  
  2. {  
  3.    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.GetConnectionString(db)))  
  4.    {  
  5.       connection.Execute(commandText, DapperHelper.GenerateParameters(model,false), commandType: commandType);  
  6.       return model;  
  7.    }  
  8. }  
  9. public List<T> Get<T>(string commandText, CommandType commandType = CommandType.StoredProcedure)  
  10.    {  
  11.    List<T> output = new List<T>();  
  12.    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.GetConnectionString(db)))  
  13.    {  
  14.       output = connection.Query<T>(commandText, commandType: commandType).AsList();  
  15.       return output;  
  16.    }  
  17. }  
To use them, for example, you have person model class look like this:
  1. public class PersonModel  
  2. {  
  3.    public int Id {get;set;}  
  4.    public string FullName {get;set;}  
  5.    public string Address {get;set;}  
  6. }  
Say that we have stored procedure called spPerson_Insert and an other one called spPerson_Get, to create new person in your database you have to do this:
  1. PersonModel p = new PersonModel { Id = 1 , FullName = "Abubakr" , Address = "Sudan" };  
  2. GlobalConfig.Connection.Create(p,"spPerson_Insert",CommandType.StoredProcedure);  
Use it the same way with the Get() method:
  1. List<PersonModel> people = GlobalConfig.Connection.Get("spPerson_Get",CommandType.StoredProcedure)  
GitHub Repository - Dapper DAL Project
 
Extension In Visual Studio Market Place - DapperDALProject
 
License
 
MIT license