Architecture for Building Back-End for Multiple Devices

Architecture for Building Back-End for Multiple Devices

Masilo Mapaila Friday, 08 April 2016

It’s becoming increasingly common to expose multiple interfaces for an application – many applications have apps for iPhone, Android, and Windows phones, in addition to the web interface. Some part of your application functionality may be exposed through other methods such as Chrome extensions or Windows tiles. Depending on the nature of your application, you may also potentially expose an API (Application Programming Interface).

To give your users a consistent experience of accessing your application from various front-ends, your application’s back-end needs to be built as a RESTful (REpresentational State Transfer) web API service. Building a Web API service will ensure the app displays the same business logic across different platforms, meaning the logic only needs to be written once.

An example of creating such a back-end is presented here.

ASP.NET Web API C# was used to build the back-end. You may ask: Why not use ASP.NET MVC? MVC is typically used for creating websites, and Controllers usually return a View (i.e. HTML response) to browser requests. Web APIs, on the other hand, are usually made to be consumed by other applications. If you want to allow other applications to access your data / functionality, you can create a Web API to facilitate this. For example, Facebook has an API that allows App developers to access relevant information about users using the App.

The image below shows, on a high level, how the back-end of our project was implemented:

Blog Image Res

The first step was to create a repository module that would be used to communicate with the database. Entity Framework was used as the object-relational mapping framework to map Entities (C# classes) to SQL tables. A repository module is used to separate the business logic from the database access code. This way, different database engines or object-relational mapping frameworks (e.g. NHibernate) can be used to connect to the database without modifying the business logic code.

The second step was to create the business logic module that would handle all API calls from different platforms. This is where the ASP.NET Web API is used. In a similar way to the MVC framework, the Web API framework also makes use of Controllers which can be created to serve different API calls. The URL to call the API is similar to the one used by MVC, with the exception of having to include the term “API”, for example: http://{domain}/api/{controller}/{action}.

Imagine building a customer support system to help call centre agents to track issues logged by customers, and tasks completed to resolve these. Using object-oriented design, there will be an Issue and Task object. Our goal is to achieve a back-end that exposes an API that can be called by multiple client applications. One of the functionalities in a support system is to be able to view details of the issue logged by the customer. This can be achieved by making a GET API call such as which will go to the IssueController inside the business logic module and will execute the Get(id) method. As a result, the Get(id) will call into the repository module to get the appropriate data. The API calls use DTOs (Data Transfer Objects) to transfer data between the back-end and the front-end. The DTOs are also C# classes whose main purpose is to transfer data. The DTOs are serialised into JSON or XML, depending on what the front-end application requires.

The third step was the creation of the actual apps on different devices that use the exposed back-end RESTful API.

Next time you want to build a cross-platform application, consider structuring your application like this to build an extensible and maintainable back-end.