Designing Better Web APIs with Breeze Server, Part 2

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


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.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist