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

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as