Hi Folks! In this post I will show you how one of my favorite design patterns work: the repository design pattern. Before you start running the application from github, you will need to import the Repository.bacpac file into your SQL Server instance. And of course, change the connection string inside the appsettings.json file in the Repository.Site project to reflect your environment.
If you are not running an instance of the SQL Server on your machine, follow this link.
You can follow this tutorial here to import the bacpac file.
Clone the repository from github here.
What I like about this design pattern is its simplicity and how it fits right into the SOLID design pattern: we will take a look at the separation of concerns that it naturally implies. Open the NET7-DesignPattern-Repository.sln solution file and go to the Repository.Data project. Browse to an interface called IBasicRepository.
As you can see in the interface, there's CRUD (Create, Read, Update and Delete) basic methods and one special method called Query() (which is kind of cool, because we are dealing with Entity Framework here), returning an instance of the IQueryable interface, letting us configure our queries however we need, before hitting the database with the SQL command. You will also notice that there's no EFContext property to be used directly in the website. That's how things/responsabilities are separated from the UI.
Now, browse to an abstract class called _BasicRepository inside the Implementations directory of the Data library. Let's take a look at how the methods are implemented: in the constructor, there's a dependecy injection for IConfiguration (because this dependency will be used by the abstract class to instantiate the EFContext class; one of the main parts of the Data library). The EFContext uses that configuration to obtain the connection string.
The Add, Delete and Update methods are pretty straightforward: they instantiate EFContext and call their respective EF methods for database changing. The Get(string where, string order) is something I need to discuss, because LINQ does not support predicates in string form. We need to install a Nuget package called System.Linq.Dynamic.Core.
Before we run the web application, let's take a look in the Program.cs class inside the Repository.Site project. You will see the DI being registered in the IOC container on lines 7 and 8. That's how ASP.NET core knows how to translate the interface from the concrete implementations.
Browse to another class in the same project: one called HomeController. You will notice that in the constructor there's 2 dependency injections: IPersonRepository and ICarRepository. They will be used by the controller to retrieve information from the data source. You will also notice that there's 2 API controllers: _CarsController and _PeopleController (I decided to name them like that to diferentiate from the standard MVC controllers).
Now run the web application (right-click on the Repository.Site project and setup as the start up project). And have fun with it!
ps.: the UI is not perfect, I rushed it, but it works perfectly as an example. It was developed using HTML, pure Javascript and jQuery (should have used Angular though 😁).
Hope you guys enjoyed it. See ya!
Gostei do artigo, muito bem escrito!