Welcome to FoxLearn
In this video, I'll show you How to use Repository Pattern with Dapper ORM in C# Windows Forms Application
First, I'll create a new Windows Forms Application project in .NET Core
If you get the same error as me, you can run the dotnet restore command to fix the problem
Next, Right-click on your project, then add an application configuration file.
You should put your connection string in the app.config file.
It makes it easy to modify the connection string without modifying your source code.
You'd better encrypt the connection string so that people can't read it.
I uploaded a video showing how to encode the connection string.
so I won't cover it in detail in this tutorial
Next, Right-click on your project, then select Manage Nuget Packages
Next, Select the Browse tab, then search for and install Dapper ORM into your project.
Dapper ORM is a high performance Micro-ORM supporting SQL Server, My SQL, Firebird etc
I will use Northwind database for practice.
This example is quite simple.
We only implement retrieving data from the Products table
We will create a product class with properties that correspond to the data fields in the product table.
Each product record corresponds to a product object
Repositories are classes or components that encapsulate the logic required to access data sources.
They centralize common data access functionality,
providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.
If you use an Object-Relational Mapper (ORM) like Entity Framework,
the code that must be implemented is simplified, thanks to LINQ and strong typing.
This lets you focus on the data persistence logic rather than on data access plumbing.
The Repository pattern is a well-documented way of working with a data source
A repository performs the tasks of an intermediary between the domain model layers and data mapping,
acting in a similar way to a set of domain objects in memory.
Client objects declaratively build queries and send them to the repositories for answers.
Conceptually, a repository encapsulates a set of objects stored in the database and operations that can be performed on them,
providing a way that is closer to the persistence layer.
Repositories, also, support the purpose of separating, clearly and in one direction,
the dependency between the work domain and the data allocation or mapping.
Next, We will create an interface.
This interface will contain methods to help us select, insert, and update data
The repository pattern is a strategy for abstracting data access.
Why would we abstract the data?
The most obvious reason is the very reason we try to reduce code duplication.
For example, If your company wants to move to MySQL to save money on licenses.
In this case, you’re going to need to do a lot of work to make these changes.
Every change to the database table requires a change to every area of your application that saves the items to that table.
Switching to My SQL would require complete rewrites since that code is locks you in to SQL Server.
It's very bad, and very risky.
This is why you should use the repository pattern.
Moreover, The Repository pattern allows you to easily test your application with unit tests.
Remember that unit tests only test your code, not infrastructure,
so the repository abstractions make it easier to achieve that goal
Next, We will create a repository class that implements the interface we just created
Next, I'll cover a few reasons why you shouldn't use it.
For example, You wouldn’t use it if your project is small,
and you truly don’t anticipate large data-level structural changes.
Another reason might be that you’re working on an application that already uses a different strategy.
If you’re working on an older project that uses a different pattern,
don’t mix the patterns unless you have an objective to separate the data access technology.
We will create an AppConnection class to help us get the connection string from the app.config file.
Next, Open your form designer,
then drag a DataGridView control from the visual studio toolbox into your form designer
We'll use the DataGridView control to display the data we get from the Products table
Next, Open your form code behind,
then we will use the Repository pattern to store and receive data from the SQL Server
As you can see, we can easily modify the source code without changing the business process too much.
Because every method we call is through an interface
Thank you for watching this video and don't forget to subscribe to my channel