Designing Better Web APIs with Breeze Server, Part 2

Configuring Breeze Controller

Continuing from where we left in the previous post, let us explore how you can use Breeze syntax to create powerful HTTP services with Web API. When you add the Breeze Server nuget package, you will notice that along with the relevant assemblies, a WebApiConfig class will also get added under the App Start folder with a Register method as shown below:

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 }            );        }

This registers the appropriate routes for Breeze. The Register method is called in the Application_Start method under Global.asax.

protected void Application_Start()        {            GlobalConfiguration.Configure(WebApiConfig.Register);        }

Now let us create a Web API Controller for an Entity Framework Model. The first step is to decorate the Controller class with the BreezeControllerAttribute. Please note that you need to have the Breeze Server for Web API 2 also installed via nuget to get the necessary artefacts for augmenting the Web API services with Breeze.

 [BreezeController]    public class SalesController : ApiController    {            }

Next, create an instance of Breeze EFContextProvider using the EF DbContext as shown in the code below:

 [BreezeController]    public class SalesController : ApiController    {        readonly EFContextProvider contextProvider =             new EFContextProvider();    }

The instance of Breeze EFContextProvider will now enable you to write methods to query the AdventureWorksEntities DbContext. What is more, it will translate your expressions into SQL Queries and execute them on the database providing optimal performance.

Methods for Querying Data

To query your entities, you can create methods that returns those entities as the EFContextProvider properties. This will allow you to write OData expressions to filter results.

// ~/breeze/sales/SalesOrders        // ~/breeze/sales/SalesOrders?$filter=[expression]        [HttpGet]        public IQueryable SalesOrders()        {            return contextProvider.Context.SalesOrderDetails;        }

Save Changes

Breeze also provides a mechanism to allow you to perform multiple insert / update operations and then persist them in the repository using SaveChanges method exposed by the EFContextProvider.

// ~/breeze/sales/SaveChanges        [HttpPost]        public SaveResult SaveChanges(JObject saveBundle)        {            return contextProvider.SaveChanges(saveBundle);        }

Metadata

One of the challenges with Web API design with RPC style implementation is that there is no metadata available that you can expose to the consumers of your service. The contract signatures have to be shared in an out-of-bounds fashion, which is a problem. Breeze allows you to expose the service metadata using the EFContextProvider Metadata method:

// ~/breeze/sales/Metadata        [HttpGet]        public string Metadata()        {            return contextProvider.Metadata();        }

You are all set now, having configured a Web API service with Breeze. If you run and browse the metadata URL you will see the method definitions listed.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS