Jeremy  Reilly

Jeremy Reilly

1599547396

Using a Dapper Base Repository in C# to Improve Readability

As I’ve mentioned in earlier posts, the project my team and I are working on has an extremely normalized database. This is both annoying and useful; annoying because many things that do not need to be SQL tables are, and useful because everything is easily found and categorized.

Photo by Markus Winkler / Unsplash

Because we are not allowed to change the SQL architecture, we are attempting to make our C## architecture both match the SQL one where possible, and improve upon it. We decided early on that Dapper was going to be our data-access tech, and that we wanted to use the Repository-Service pattern that I’ve written about before.

So, in the interest of better code readability, we tried to see if it was possible to create a “layer” of our architecture below the repository layer. Said layer would be a “base” class that all other repositories would inherit from, and the methods would need to be generic.

We did find a way, and we think that way will be useful to more than just our team; hence, this post. Let’s see how to use a Dapper Base Repository to improve our code’s readability!

Anatomy of a Query Method

Here’s an example of a simple Query base method.

using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;

namespace DapperBaseRepositoryDemo.Repositories
{
    public class DapperBaseRepository
    {
        public List<T> Query<T>(string query, object parameters = null)
        {
            try
            {
                using (SqlConnection conn 
                       = new SqlConnection("Your Connection String"))
                {
                    return conn.Query<T>(query, parameters).ToList();
                }
            }
            catch (Exception ex)
            {
                //Handle the exception
                return new List<T>();
            }
        }
    }
}

The goal of these kinds of methods is to be as generic as possible, so that they are useful in as many situations as possible.

#c# #c++

What is GEEK

Buddha Community

Using a Dapper Base Repository in C# to Improve Readability
Jeremy  Reilly

Jeremy Reilly

1599547396

Using a Dapper Base Repository in C# to Improve Readability

As I’ve mentioned in earlier posts, the project my team and I are working on has an extremely normalized database. This is both annoying and useful; annoying because many things that do not need to be SQL tables are, and useful because everything is easily found and categorized.

Photo by Markus Winkler / Unsplash

Because we are not allowed to change the SQL architecture, we are attempting to make our C## architecture both match the SQL one where possible, and improve upon it. We decided early on that Dapper was going to be our data-access tech, and that we wanted to use the Repository-Service pattern that I’ve written about before.

So, in the interest of better code readability, we tried to see if it was possible to create a “layer” of our architecture below the repository layer. Said layer would be a “base” class that all other repositories would inherit from, and the methods would need to be generic.

We did find a way, and we think that way will be useful to more than just our team; hence, this post. Let’s see how to use a Dapper Base Repository to improve our code’s readability!

Anatomy of a Query Method

Here’s an example of a simple Query base method.

using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;

namespace DapperBaseRepositoryDemo.Repositories
{
    public class DapperBaseRepository
    {
        public List<T> Query<T>(string query, object parameters = null)
        {
            try
            {
                using (SqlConnection conn 
                       = new SqlConnection("Your Connection String"))
                {
                    return conn.Query<T>(query, parameters).ToList();
                }
            }
            catch (Exception ex)
            {
                //Handle the exception
                return new List<T>();
            }
        }
    }
}

The goal of these kinds of methods is to be as generic as possible, so that they are useful in as many situations as possible.

#c# #c++

Pass method as parameter using C# | Delegates in C# | C# Bangla Tutorial | Advanced C#

https://youtu.be/GfcTSJf5Rc8

#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #pass method as parameter using c#

Ari  Bogisich

Ari Bogisich

1589816580

Using isdigit() in C/C++

In this article, we’ll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let’s look at how to use this function, using some simple examples.

#c programming #c++ #c #c#

Ari  Bogisich

Ari Bogisich

1590565090

A Guide to using the strdup() function in C/C++

In this article, we’ll take a look at using the strdup() function in C/C++.

The strdup() function is very useful if you want to duplicate the contents of a string onto another string.

Let’s see how we can utilize this function, using some simple examples.

#c programming #c++ #c #c#

Tamale  Moses

Tamale Moses

1624240146

How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++