Package | Versions |
---|---|
Tagada | |
Tagada.Swagger | |
Visual Studio extension (project templates) |
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
- 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()
orBeforeEach<T>()
- Execute code
AfterEach()
orAfterEach<T>()
- Add swagger documentation
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
}
public void Configure(IApplicationBuilder app)
{
app.Map("/api")
.Get("/hello", () => "Hello world!")
.Use();
}
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();
}
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; }
}
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();
}
- 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;
- 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;
};
List of tools / articles that inspired me to write this library:
- https://www.strathweb.com/2017/01/building-microservices-with-asp-net-core-without-mvc/
- https://github.com/filipw/aspnetcore-api-samples/blob/master/01%20Lightweight%20API%20(no%20MVC)/LightweightApi/Program.cs
- http://www.koderdojo.com/blog/asp-net-core-routing-and-routebuilder-mapget-for-calculating-a-factorial