<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Zone of Development</title>
    <description>The latest articles on Forem by Zone of Development (@zoneofdevelopment).</description>
    <link>https://forem.com/zoneofdevelopment</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F889985%2Ff6319a62-dde0-44a0-9061-f27ca8be4aaf.jpeg</url>
      <title>Forem: Zone of Development</title>
      <link>https://forem.com/zoneofdevelopment</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/zoneofdevelopment"/>
    <language>en</language>
    <item>
      <title>Minimal API with ASP.NET Core</title>
      <dc:creator>Zone of Development</dc:creator>
      <pubDate>Sun, 31 Jul 2022 20:00:25 +0000</pubDate>
      <link>https://forem.com/zoneofdevelopment/minimal-api-with-aspnet-core-1139</link>
      <guid>https://forem.com/zoneofdevelopment/minimal-api-with-aspnet-core-1139</guid>
      <description>&lt;p&gt;In this post, we will see how to create a Minimal API with .net Core.&lt;br&gt;
But first of all, what is a Minimal API?&lt;br&gt;
From &lt;a href="https://docs.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-6.0&amp;amp;tabs=visual-studio"&gt;Microsoft web site&lt;/a&gt;:&lt;br&gt;
“Minimal APIs are architected to create HTTP APIs with minimal dependencies. They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.”&lt;br&gt;
In a nutshell, we can use Minimal API when we need to create simple services with less complexity, layers and classes.&lt;/p&gt;

&lt;p&gt;For this post, we will develop a Minimal API for managing the CRUD operations of an entity called Dog.&lt;br&gt;
We start opening VS and we create an ASP.NET Core Web API project called MinimalAPI with this properties:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nz6VJbqn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cig59osr03hf4gkwbmp5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nz6VJbqn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cig59osr03hf4gkwbmp5.png" alt="Visual Studio project definition" width="820" height="625"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, we will install the “Microsoft Entity FrameworkCore InMemory” library, running from the Package Manager Console the command:&lt;br&gt;
&lt;code&gt;Install-Package Microsoft.EntityFrameworkCore.InMemory&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, we will create the Business Layer to manage CRUD operations:&lt;br&gt;
&lt;strong&gt;[DOG.CS]&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace MinimalAPI.Model
{
    public class Dog
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Breed { get; set; }
        public string Color { get; set; }   
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;[DATACONTEXT.CS]&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.EntityFrameworkCore;

namespace MinimalAPI.Model
{
    public class DataContext: DbContext
    {
        public DataContext(DbContextOptions&amp;lt;DataContext&amp;gt; options)
        : base(options) { }

        public DbSet&amp;lt;Dog&amp;gt; Dogs =&amp;gt; Set&amp;lt;Dog&amp;gt;();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;[IDOGCOMMANDS.CS]&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using MinimalAPI.Model;

namespace MinimalAPI.Commands
{
    public interface IDogCommands
    {
        Task&amp;lt;bool&amp;gt; AddDog(Dog dog);

        Task&amp;lt;List&amp;lt;Dog&amp;gt;&amp;gt; GetAllDogs();

        Task&amp;lt;Dog&amp;gt; GetDogById(Guid id);

        Task&amp;lt;bool&amp;gt; UpdateDog(Dog dog, Guid id);

        Task&amp;lt;bool&amp;gt; DeleteDog(Guid id);

        Task Save();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;[DOGCOMMANDS.CS]&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.EntityFrameworkCore;
using MinimalAPI.Model;

namespace MinimalAPI.Commands
{
    public class DogCommands:IDogCommands
    {
        private readonly DataContext _dataContext;

        public DogCommands(DataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public async Task&amp;lt;bool&amp;gt; AddDog(Dog dog)
        {
            try
            {
                await _dataContext.Dogs.AddAsync(dog);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public async Task&amp;lt;List&amp;lt;Dog&amp;gt;&amp;gt; GetAllDogs()
        {
            return await _dataContext.Dogs.AsNoTracking().ToListAsync();
        }

        public async Task&amp;lt;Dog&amp;gt; GetDogById(Guid id)
        {
            return await _dataContext.Dogs.FindAsync(id);
        }

        public async Task&amp;lt;bool&amp;gt; UpdateDog(Dog dog, Guid id)
        {
            var dogInput = await _dataContext.Dogs.FindAsync(id);

            if(dogInput == null)
            {
                return false;
            }

            dogInput.Name = dog.Name;
            dogInput.Color = dog.Color;
            dogInput.Breed = dog.Breed;

            await _dataContext.SaveChangesAsync();

            return true;
        }

        public async Task&amp;lt;bool&amp;gt; DeleteDog(Guid id)
        {
            var dogInput = await _dataContext.Dogs.FindAsync(id);

            if (dogInput == null)
            {
                return false;
            }

            _dataContext.Dogs.Remove(dogInput);
            return true;
        }

        public async Task Save()
        {
            await _dataContext.SaveChangesAsync();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we modify Program.cs where we will add the API code:&lt;br&gt;
&lt;strong&gt;[PROGRAM.CS]&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.EntityFrameworkCore;
using MinimalAPI.Commands;
using MinimalAPI.Model;

var builder = WebApplication.CreateBuilder(args);
// definition of DataContext
builder.Services.AddDbContext&amp;lt;DataContext&amp;gt;(opt =&amp;gt; opt.UseInMemoryDatabase("DbDog"));
// definition of Dependency Injection
builder.Services.AddScoped&amp;lt;IDogCommands, DogCommands&amp;gt;();

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

// Definition Get Method
app.MapGet("/dog", async (IDogCommands commands) =&amp;gt;
    await commands.GetAllDogs());

// Definition Get{Id} Method
app.MapGet("/dog/{id}", async (Guid id, IDogCommands commands) =&amp;gt;
{
    var dog = await commands.GetDogById(id);

    if (dog == null) return Results.NotFound();

    return Results.Ok(dog);
});

// Definition Post Method
app.MapPost("/dog", async (Dog dog, IDogCommands commands) =&amp;gt;
{
    await commands.AddDog(dog);
    await commands.Save();

    return Results.Ok();
});

// Definition Put Method
app.MapPut("/dog/{id}", async (Guid id, Dog dog, IDogCommands commands) =&amp;gt;
{
    var updateOk = await commands.UpdateDog(dog, id);

    if (!updateOk) return Results.NotFound();

    return Results.NoContent();
});

// Definition Delete Method
app.MapDelete("/dog/{id}", async (Guid id, IDogCommands commands) =&amp;gt;
{
    var deleteOk = await commands.DeleteDog(id);
    if (deleteOk)
    {
        await commands.Save();
        return Results.Ok();
    }

    return Results.NotFound();
});

app.Run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;We have done and now, if we run the application, this will be the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WzG_jx1E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q0f5qi3o8din1av7yav.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WzG_jx1E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q0f5qi3o8din1av7yav.png" alt="Swagger" width="880" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ADD DOG&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WOq-IGXx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngug297vmpxeefejtw8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WOq-IGXx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngug297vmpxeefejtw8p.png" alt="Testing add operation" width="880" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kLF57tpL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65kde4qynjykarivx6gv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kLF57tpL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65kde4qynjykarivx6gv.png" alt="Testing add operation" width="880" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8z4f_-du--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4p8jcq64753158s2w75v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8z4f_-du--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4p8jcq64753158s2w75v.png" alt="Testing add operation" width="880" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GET ALL DOGS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kRdzxJnl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnlztdz6fe3m82l9u0dl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kRdzxJnl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnlztdz6fe3m82l9u0dl.png" alt="Testing get operation" width="880" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE DOG&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GeIvX9OF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea14i67j72lj3ebdcyep.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GeIvX9OF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea14i67j72lj3ebdcyep.png" alt="Testing update operation" width="880" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3qAcSMra--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5445ui0ej63hmmumu8v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3qAcSMra--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5445ui0ej63hmmumu8v.png" alt="Testing update operation" width="880" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--__LtKt8s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmu65j6ujxfxzaxd3lx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--__LtKt8s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmu65j6ujxfxzaxd3lx1.png" alt="Testing update operation" width="880" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DELETE DOG&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oz8nORdY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qkmsonoi3t2pw5cyl2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oz8nORdY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qkmsonoi3t2pw5cyl2g.png" alt="Testing delete operation" width="880" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n8sgESxY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxl491nltgwazml5lfr1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n8sgESxY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxl491nltgwazml5lfr1.png" alt="Testing delete operation" width="880" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>core</category>
      <category>webapi</category>
      <category>restful</category>
      <category>microsoft</category>
    </item>
  </channel>
</rss>
