Skip to content

WebApi Stems

Connell edited this page Jan 21, 2019 · 2 revisions

Adding Firestorm Stems to an existing ASP.NET application with Entity Framework

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 repository

  1. Clone the BookService GitHub repository and open the solution.
  2. Run the Entity Framework Migrations from the tutorial.

Add Firestorm packages

PM> Install-Package Firestorm.AspNetWebApi2
PM> Install-Package Firestorm.Stems
PM> Install-Package Firestorm.EntityFramework6

Add Firestorm to the Web API config

  1. Open App_Start/WebApiConfig.cs
  2. 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 the Stems

  1. Create a new folder in the project root called Stems.
  2. Create a new C# class file in here called AuthorsStem.cs.
  3. 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; }
    }
}
  1. Create a new C# class file in here called BooksStem.cs.
  2. 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.

Switch front-end to use Firestorm

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.

  1. Open Scripts/app.js.
  2. Replace the Uri variables defined near the top with the Firestorm versions.
    var booksUri = '/api2/books/';
    var authorsUri = '/api2/authors/';

Run your solution

Run the solution. Everything should be working as before, but now using Firestorm.

Clone this wiki locally