Hello friends, here I will show you how to create a WebApi with the following characteristics:
- ASP.Core 2.1
- EntityFramework
- FluentValidation
- Nlogger
- Swagger
- Jwt
Let's start. First create an empty project, then add the following folders:
- Application
- Domain
- Service
- Infrastructure
Then in the Domain folder, we create a library project Net.Core 2.1 with the name WebApi.Domain add the following dependencies
FluentValidation.AspNetCore
In this project, we add the following folders:
In the Entities folder, we create the BaseEntity class:
namespace WebApi.Domain.Entities
{
public abstract class BaseEntity
{
public virtual int Id { get; set; }
}
}
Our project classes will inherit the field Id from this abstract class (if you want you can add other fields like CreatedAt or CreatedBy).
Then we create the Country class with the properties that defines a Country.
namespace WebApi.Domain.Entities
{
public class Country : BaseEntity
{
public string Name { get; set; }
public int Population { get; set; }
public decimal Area { get; set; }
public string ISO3166 { get; set; }
public string DrivingSide { get; set; }
public string Capital { get; set; }
}
}
Now to make the exercise more interesting, we are going to assume that we do not want to expose all the Country class. In the Dtos folder, we create the following CountryDensityDTO class.
using System;
using WebApi.Domain.Entities;
namespace WebApi.Domain.Dtos
{
public class CountryDensityDTO : BaseEntity
{
public string Name { get; set; }
public string Capital { get; set; }
public decimal Area { get; set; }
public int Population { get; set; }
public int Populationdensity
{
get
{
return Decimal.ToInt32(Population / Area);
}
}
}
}
This class exposes Name, Capital Area, Population and a calculated field Populationdensity.
Now we will continue with the Infrastructure layer and then we will finish the missing parts.We go to the Infrastructure folder and create a library Net.Core 2.1. We name it WebApi.Infrastructure.Data.
We add the following Packages:
- Microsoft.EntityFrameworkCore.SqlServer 2.1.4
- Microsoft.EntityFrameworkCore.Tools 2.1.4
- Microsoft.Extensions.Identity.Stores 2.1.1
- Microsoft.VisualStudio.Web.CodeGeneration.Design 2.1.5
- Add Project reference WebApi.Domain
We create the following folders:
- Context
- EntityDbMapping
- Repository
In the Context Folder, we add the SqlServerContext class. We refer to our Country entity with DbSet to work with the database. As we work with CodeFirst approach, we will create a mapping for our entity Country in the database.
"modelBuilder.Entity<Country>(new CountryMap().Configure);"
Optionally, in this part we can also add seed data when creating a table.
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using WebApi.Domain.Entities;
using WebApi.Infrastructure.Data.EntityDbMapping;
namespace WebApi.Infrastructure.Data.Context
{
public class SqlServerContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Country> Country { get; set; }
public SqlServerContext(DbContextOptions<SqlServerContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Country>(new CountryMap().Configure);
// ModelBuilderExtensions.Seed(modelBuilder);
}
}
//Data for first time on table
public static class ModelBuilderExtensions
{
public static void Seed(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>().HasData(
new Country
{
Id = 1,
Name = "Venezuela",
Population = 300000000,
Area = 230103
},
new Country
{
Id = 2,
Name = "Peru",
Population = 260000000,
Area =33249
}
);
}
}
}
In the folder, EntityDbMapping, we create the CountryMap class. In this class, we define the physical representation of the properties of the Country class as fields in the table of the database.
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WebApi.Domain.Entities;
namespace WebApi.Infrastructure.Data.EntityDbMapping
{
public class CountryMap : IEntityTypeConfiguration<Country>
{
public void Configure(EntityTypeBuilder<Country> builder)
{
builder.ToTable("Country");
builder.HasKey(c => c.Id);
builder.Property(c => c.Name)
.IsRequired()
.HasColumnName("Name")
.HasColumnType("varchar(150)");
builder.Property(c => c.Population)
.IsRequired()
.HasColumnType("int")
.HasColumnName("Population");
builder.Property(c => c.Area)
.IsRequired()
.HasColumnType("decimal(14,2)")
.HasColumnName("Area");
builder.Property(c => c.ISO3166)
.IsRequired()
.HasColumnType("varchar(3)")
.HasColumnName("ISO3166");
builder.Property(c => c.DrivingSide)
.IsRequired()
.HasColumnType("varchar(50)")
.HasColumnName("DrivingSide");
builder.Property(c => c.Capital)
.IsRequired()
.HasColumnType("varchar(50)")
.HasColumnName("Capital");
}
}
}
This is it for now.
In the next chapter, we will implement validations with FluentValidation. We will also configure Mapper to use it with our DTOs and will implement Identity using Jwt.