In this article, we will create our second microservice application that contains the orders endpoint.
Part-2 Create A Microservice For The Products Endpoint[.NET6 Microservice Series]
Now add the Database Context class like 'SalesBusinessContext.cs' inside of the 'Data' folder.
Step 3:
Step 1:
The microservice application consists of simple or single module logic endpoints. So here I'm going to create 2 endpoints like HTTP GET, HTTP POST for the 'Orders' to accomplish our demo.
Create A .NET6 Web API Application:
Let's create our second microservice application with .NET6 Web API application. Let's name our microservice project 'SalesBusiness.Api'. To create .NET6 application recommended IDE's are 'Visual Studio 2022', 'Visual Studio Code'. In this demo, I'm using .NET CLI and Visual Studio Code editor.
CLI command
dotnet new webapi -o Your_Project_Name
dotnet new webapi -o Your_Project_Name
Install NuGet Packages:
Install EntityFramework Core NuGet.
.NET CLI command
dotnet add package Microsoft.EntityFrameworkCore --version 6.0.3
dotnet add package Microsoft.EntityFrameworkCore --version 6.0.3
Package Manager command
Install-Package Microsoft.EntityFrameworkCore -Version 6.0.3
Install-Package Microsoft.EntityFrameworkCore -Version 6.0.3
Install EntityFramework Core SqlServer NuGet.
.NET CLI command
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.3
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.3
Package Manager command
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.3
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.3
Setup Database Context:
Let's add an entity for our 'Orders' table as 'Orders.cs' file inside of the 'Data/Entities' folder.
Data/Entities/Orders.cs:
namespace SalesBusiness.Api.Data.Entities; public class Orders { public int Id { get; set; } public string UserId { get; set; } public int ProductId { get; set; } public DateTime? OrderDate { get; set; } }
- Here 'Orders' table contains the property 'ProductId' other than that we don't have any information about the 'Products', So in next article(Part-4) we are going to understand how to fetch product information for the 'Orders'.
Data/SalesBusinessContext.cs:
using Microsoft.EntityFrameworkCore; using SalesBusiness.Api.Data.Entities; namespace SalesBusiness.Api.Data; public class SalesBusinessContext: DbContext { public SalesBusinessContext(DbContextOptions<SalesBusinessContext> options):base(options) { } public DbSet<Orders> Orders{get;set;} }
- (Line: 5) To make the class a database context class it must inherit the 'Microsoft.EntityFrameworkCore.DbContext'.
- (Line: 11) Register the table class inside of the database context as 'DbSet<TEnity>'
appsettings.Development.json:
"ConnectionStrings": { "SalesBusinessDBConnection":"your connection string" }Now register our 'SalesBusinessContext' into the 'Program.cs'.
Program.cs:
builder.Services.AddDbContext<SalesBusinessContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("SalesBusinessDBConnection")); });
Add OrdersController.cs:
Let's create our endpoint by adding the 'OrdersController.cs'.
Controllers/OrdersController.cs:
using Microsoft.AspNetCore.Mvc; using SalesBusiness.Api.Data; namespace SalesBusiness.Api.Controllers; [ApiController] [Route("[controller]")] public class OrdersController:ControllerBase { private readonly SalesBusinessContext _salesBusinessContext; public OrdersController(SalesBusinessContext salesBusinessContext) { _salesBusinessContext = salesBusinessContext; } }
- Here in our controller constructor we injected the 'SalesBusinessContext'.
Implement Create Operation:
Let's implement create operation endpoint. On creating an item in the response it is always a good idea to return the endpoint URL to get an item by its 'Id' value. So now we will create 2 action methods one for creating the item another to get an item by 'Id'.
Controllers/OrdersController.cs:
[HttpGet] [Route("{id}")] public async Task<IActionResult> GetAsync(int id) { var order = await _salesBusinessContext.Orders.FindAsync(id); return Ok(order); } [HttpPost] public async Task<IActionResult> PostAsync(Orders newOrder) { _salesBusinessContext.Orders.Add(newOrder); await _salesBusinessContext.SaveChangesAsync(); return CreatedAtAction("Get", new { id = newOrder.Id }, newOrder); }
- (Line: 1-7)The get action method to fetch the item by 'Id'.
- (Line: 2) Here in the 'Route' attribute added dynamic 'Id' value its value will be passed to the action method input parameter.
- (Line: 5) Fetching the 'Orders' by filtering it by 'Id'.
- (Line: 9-15) The post-action method to create a new item into the database.
- (Line: 12-13) Using 'SalesBusinessContext' to save the item into the database.
- (Line: 14) The 'CreatedAtAction' method generates the get endpoint and the endpoint will be returned as the header value of the response.
Step 3:
Implement Read Operation:
Let's implement a read operation to fetch all the 'Orders' from the database.
Controllers/ProductsController.cs:
[HttpGet] public async Task<IActionResult> GetAsync() { var orders = await _salesBusinessContext.Orders.ToListAsync(); return Ok(orders); }
- Here action method returns all 'Orders' item as a response.
Step 2:
So that's about the 'SalesBusiness.API' microservice application that contains the 'Orders' endpoint. In the next article, we are going to use the RabittMq message broker to establish communication between our microservice application for asynchronous data sharing.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information Microservice flow in .NET6 applications. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Refer:
Part -1 Introduction On Microservice And SQL Database Design For Microservices [.NET 6 MicorService Series]
Part-2 Create A Microservice For The Products Endpoint[.NET6 Microservice Series]
Part-4 Asynchronous Data Communication Between Microservices Using RabbitMQ Message Broker With MassTransit[.NET6 Microservice Series]
Part-5 Ocelot API Gateway For Microservices[.NET6 Microservice Series]
Git Source
Part-2 Create A Microservice For The Products Endpoint[.NET6 Microservice Series]
Part-4 Asynchronous Data Communication Between Microservices Using RabbitMQ Message Broker With MassTransit[.NET6 Microservice Series]
Part-5 Ocelot API Gateway For Microservices[.NET6 Microservice Series]
Git Source
Comments
Post a Comment