Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
/ tagada Public archive

For those who dream to make an ASP.NET Core Web API in one line of code

License

Notifications You must be signed in to change notification settings

Odonno/tagada

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tagada

CodeFactor

Package Versions
Tagada NuGet
Tagada.Swagger NuGet
Visual Studio extension (project templates) VSMarketplace

Tagada is a lightweight functional framework to create a .NET Core Web API without effort. And of course it tastes good.

For those who dream to make an ASP.NET Core Web API in one line of code

Features

  • Add routes based on HTTP methods GET, POST, PUT, DELETE
  • Add routes based on generic input and output <TQuery, TResult> or <TCommand, TResult>
  • Execute code BeforeEach() or BeforeEach<T>()
  • Execute code AfterEach() or AfterEach<T>()
  • Add swagger documentation

Get started

A simple Hello World

public void ConfigureServices(IServiceCollection services)
{
    services.AddRouting();
}

public void Configure(IApplicationBuilder app)
{
    app.Map("/api")
        .Get("/hello", () => "Hello world!")
        .Use();
}

Add Swagger documentation

public void ConfigureServices(IServiceCollection services)
{
    services.AddRouting();

    services.AddSwaggerGen(c =>
    {
        c.GenerateTagadaSwaggerDoc();
    });
}

public void Configure(IApplicationBuilder app)
{
    app.Map("/api")
        .Get("/hello", () => "Hello world!")
        .AddSwagger()
        .Use();
}

CQRS-ready

public void ConfigureServices(IServiceCollection services)
{
    services.AddRouting();
}

public void Configure(IApplicationBuilder app)
{
    app.Map("/api")
        .Get("/contacts", GetContacts)
        .Get("/contacts/{id}", GetContactById)
        .Post("/contacts", CreateContact)
        .Use();
}
public class GetContactsQuery { }

public class GetContactByIdQuery
{
    public int Id { get; set; }
}

public class CreateContactCommand
{
    public string Name { get; set; }
}

Storing Commands in a single line of code

public void ConfigureServices(IServiceCollection services)
{
    services.AddRouting();
}

public void Configure(IApplicationBuilder app)
{
    app.Map("/api")
        .Get("/contacts", GetContacts)
        .Get("/contacts/{id}", GetContactById)
        .Post("/contacts", CreateContact)
        .AfterEach(routeResult => QueriesOrCommands.Add(routeResult.Input))
        .Use();
}

Writing queries

  • Return result without parameters
.Get("/hello", () => "Hello world!")
  • Return result from query (extracted from parameters)
.Get("/add/{number1}/{number2}", (AddNumbersQuery query) => query.Number1 + query.Number2)
  • Return result from query with a function
.Get("/contacts", GetContacts)
public static Func<GetContactsQuery, IEnumerable<Contact>> GetContacts = _ => Contacts;

Writing commands

  • Command without result
.Post<Command>("/command", Dispatch)
public static void Dispatch(Command command)
{
    Commands.Add(command);
}
  • Command with a result
.Post("/contacts", CreateContact)
public static Func<CreateContactCommand, Contact> CreateContact = command =>
{
    var newContact = new Contact
    {
        Id = Contacts.Count + 1,
        FirstName = command.FirstName,
        LastName = command.LastName
    };

    Contacts.Add(newContact);

    return newContact;
};

Resources

List of tools / articles that inspired me to write this library: