6 Tips dan Trik Memilih Skincare yang Tepat

Memilih skincare yang cocok buat kulit tuh bagaikan mencari cinta sejati, perlu effort dan waktu, bahkan gak jarang juga mengalami kegagalan. Eits, tapi gak perlu khawatir. Ada cara buat mengurangi…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Resources

This article is about CRUD operation based on MongoDB and the Repository pattern and how we can implement this process in .Net 7. In Part 1 and Part 2, CRUD operation had been implemented by SQL server and MySQL and now in the last part, it will be implemented by MongoDB.

Pre-requisites

First, we need to know a little about the differences between NoSQL and SQL-based databases to be more precise and figure out which databases could be helpful based on our scenario.

So, let’s review the significant differences between these two kinds of databases.

SQL databases are table base and relational and use tables and columns and data stored inside the table’s row. On the other hand, NoSQL databases are not table based and data stored in 4 structure such as Column-Oriented (e.g.: MariaDB), Key-Value stores (e.g.: Redis), Document stores (e.g.: MongoDB), Graph databases (e.g.: Neo4j, RedisGraph) (1). The other difference is Scalability. SQL databases are Vertical (scale-up with a larger server) and NoSQL databases are Horizontal (scale-out across commodity servers) (2).

After basic knowledge about NoSQL databases, let’s have an explanation of MongoDB.

MongoDB is a document database and unlike SQL database, all data is stored in a JSON-like document (BSON) and the structure of these JSON-like documents could be changed all the time. This flexibility helps to modify the data and their structure, add more fields, etc. Furthermore, the document model in MongoDB maps to the object that has been added to your application code. (3)

After the above explanation, let’s move forward to the implementation of CRUD operation using the Repository pattern in .Net 7.

Step One

To pull and run the MongoDB image add the following commands inside the terminal in VSCode or Command Prompt.

MongoDB on Docker-Desktop

As shown in the above image, MongoDB has been run on the Docker desktop. Now it is usable in GUI software like MongoDB Compass or any MongoDB extension in VSCode like MongoDB for VS Code.

MongoDB Compass
MongoDB Compass — Databases

This GUI will demonstrate the collection that will be stored from our application. Now, in the next step, we can start the implementation of our application based on the Repository.

Step Two

First, create a new web API project in VSCode using the following command in the terminal.

Second, the MongoDB package is an essential package that needs to use inside the API. So, add the below command in the terminal of VSCode.

Step Three

First of all, the configuration of MongoDB should be added to the project. So, add the below section in the appsettings.json file.

Now, add a new folder in the root of the project and call it Models, then add the below class as a configuration class that holds the connection string and database name.

MongoDBSettings.cs

Then, like in the previous parts, register MongoDB in Program.cs file.

Step Four

In the Models folder, add Music.cs file which represents the model.

Music.cs

Unlike using SQL Database in .Net, DbContext class is not required since EF core is not a part of the application.

Now the repository should be implemented. So, in the root folder add a new folder and call it Data and, in that folder, add another folder and call it MusicRepo. As we mentioned in the previous parts, Interface and a class are essential parts of the Repository pattern. So, add IMusicService.cs in the MusicRepo folder.

IMusicService.cs

Then, add the repository and implement all methods for the CRUD operation.

MusicService.cs

As shown in the above code, there are 4 general functions that aid to provide the CRUD operation Such as Find, ReplaceOne, DeleteOne, and InsertOne. The significant note that there is in MusicService.cs file is about the injection since the collection sets in here.

Step Five

In this step, the DTO object and Automapper are the requirements that should be added to the project. As known, when using MongoDB, it is not necessary to use mapping libraries like AutoMapper since the data will be mapped automatically in MongoDB. But what should be done when there will be needed to get some information in another type like First name and last name and send it as Fullname to the repository? Yes. One of the methods that this problem will be solved is using mapping libraries.

Therefore, add the following commands to add AutoMapper packages.

Now, register Automapper in Program.cs file.

Then add a new folder in the root of the project and call it DTO. Inside it, adds a class and calls it MusicDTO.cs, and adds the following properties inside it. To keep the example simple, all properties that have been used inside the base model are going to be set.

MusicDTO.cs

Now, add a new folder in the root of the project and call MapModel and inside this folder add a new class and call it MusicMapper.cs. This class represents the configuration of AutMapper.

MusicMapper.cs

There is a complete explanation of DTO and AutoMapper and why we should use these inside the API. In Part 1 and Part 2, all sources have been mentioned at the bottom of those articles. To make yourself clearer about these, you can check those parts.

Step Six

In the final step of the implementation of the CRUD operation, the endpoints should be added to the controllers. So, in the Controllers folder, add MusicController.cs and add the following codes inside it. First, inject IMapper from Automapper and IMusicService inside the constructor and then use these to implement CRUD endpoints.

MusicController.cs

Now we can run the API and check endpoints using Postman, Swagger, or any other software.

Add Music
Edit Music
Delete Music
List Music
Details Music

These 3 parts were an explanation of repository pattern with 3 different databases.

Add a comment

Related posts:

Why is A Powerful Woman?

When you hear which “powerful woman”, precisely what picture comes to head? Do you envision a lady who you feel comes with attained success, or maybe a woman who has applied on a battle with some…

Capture Memories in Stunning Detail with 3D Crystal Photo Gifts Online

As we move forward in the digital age, capturing memories has become easier than ever. With the convenience of smartphones and digital cameras, we are able to take pictures of everything from the…

Support the Arts in Oregon through artPAC

A former mayor of Portland, Sam Adams helped the city achieve its status as one of the most sustainable cities in the country before moving on to executive leadership for nonprofit organizations. Sam…