-
Notifications
You must be signed in to change notification settings - Fork 0
WebApi Stems
Connell edited this page Jan 21, 2019
·
2 revisions
This tutorial sets up a simple REST API using Stems.
We will hook onto an existing application built with Web API 2.0 and Entity Framework 6, from this tutorial.
- Clone the BookService GitHub repository and open the solution.
- Run the Entity Framework Migrations from the tutorial.
PM> Install-Package Firestorm.AspNetWebApi2
PM> Install-Package Firestorm.Stems
PM> Install-Package Firestorm.EntityFramework6
- Open App_Start/WebApiConfig.cs
- Add Firestorm to the config using the extension.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace BookService
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Firestorm
config.SetupFirestorm("api2", o => o
.AddEndpoints()
.AddStems()
.AddEntityFramework<BookServiceContext>()
);
}
}
}
- Create a new folder in the project root called Stems.
- Create a new C# class file in here called AuthorsStem.cs.
- Replace the contents of the file with the following code.
using BookService.Models;
using Firestorm.Stems;
using Firestorm.Stems.Attributes.Basic.Attributes;
using Firestorm.Stems.Roots.DataSource;
namespace BookService.Stems
{
[DataSourceRoot]
public class AuthorsStem : Stem<Author>
{
[Identifier, Get]
public static int Id { get; set; }
[Get, Set]
public static string Name { get; set; }
}
}
- Create a new C# class file in here called BooksStem.cs.
- Replace the contents of the file with the following code.
using Firestorm.Stems;
using Firestorm.Stems.Attributes.Basic.Attributes;
using Firestorm.Stems.Attributes.Definitions;
using Firestorm.Stems.Roots.DataSource;
namespace BookService.Stems
{
[DataSourceRoot]
public class BooksStem : Stem<Book>
{
[Identifier, Get(Display.Nested)]
public static int Id { get; set; }
[Get(Display.Nested), Set]
public static string Title { get; set; }
[Set]
public static int AuthorId { get; set; }
[Get(Display.Nested)]
public static Expression<Func<Book, string>> AuthorName => b => b.Author.Name;
[Get, Set]
public static int Year { get; set; }
[Get, Set]
public static decimal Price { get; set; }
[Get, Set]
public static string Genre { get; set; }
}
}
These Stems will do the same thing as the BooksController
, AuthorsController
and the DTOs.
Now you can use Firestorm instead of the Web API controllers.
We currently have both APIs running under different URLs. Firestorm is running under /api2/
, so let's just switch the roots in the JavaScript.
- Open Scripts/app.js.
- Replace the
Uri
variables defined near the top with the Firestorm versions.
var booksUri = '/api2/books/';
var authorsUri = '/api2/authors/';
Run the solution. Everything should be working as before, but now using Firestorm.