📜 ⬆️ ⬇️

From Phone to Cloud: Azure Mobile Services. Where to begin?


Author: Anastasia Belokurova, .NET Developer DataArt.

I'll tell you how and why to use the Azure Mobile Services cloud system for your mobile apps. To do this, we will analyze a small application, by the example of which we will see how this all works, we will touch on both practice and theory.

But before that, let's remember what the benefits of cloud technologies are and how these technologies relate to mobile devices.
')
Cloud Benefits

The term "cloud computing" appeared quite a long time ago, but its meaning changed a little. At first cloud computing was called simply distributed computing. But now the cloud platform is a large set of convenient services that you can use to solve your problems.

So why clouds? They provide the following benefits to your application:


Imagine that you run a small online store - first you just want to check whether it will be in general in demand. Your budget is small. Therefore, you write a small web application and place it on your computer, which you do not turn off at night. Your site has access from the outside, and it can withstand, say, 10 users at a time. Over time, your site becomes popular, and you already need it to withstand 100 users at a time. But your computer is not able to cope with such a load. And then you start to think, can I turn my apartment into a server room? .. No, this is terribly inconvenient! Then you buy the server provided by the hosting. Now your site supports 100 requests per minute and even a thousand. Finally, at some point you realize that you are entering the global market, because you have a lot of users around the clock, and the servers that you bought are no longer cope.

Here we come to the first advantage of cloud platforms - they provide the scalability of almost everything at almost any time. And this is done very simply, quickly and conveniently: you only have to think about development, and not about placement and load. This is the first advantage.

The second. Suppose you have a successful project with many visitors and users hosted on servers. And then a meteorite falls on your servers or something else happens. And in general all the data of your project was stored on them ... Meanwhile, most of the cloud platforms support replication: you choose the region in which the application will reside, but it will automatically be duplicated on servers in some other region. Therefore, cloud providers always securely store your data: they promise that in force majeure situations they will be restored in about 20 minutes. You will never achieve such reliable data protection if you store your data in one place.

The third is also a very important advantage: now most of the cloud providers provide many bicycle services that can be used. This is the code that we can use in each project. Therefore, we can not write the same code every time, not build the same architecture - much of this is provided to us. We can take advantage of this and think only about the specific things that are needed for a specific project.

Mobile devices and cloud technologies

How are mobile devices connected to clouds?

Many mobile applications work only with connected Internet. Therefore, they can use logic that is not located on the phone, but is located somewhere on the servers - this saves device resources.

It is worth noting that now there is a tendency to increase the use of mobile devices in everyday life - one person may even have several such devices. And, of course, convenient if the data between all these mobile devices are synchronized. If all our devices, for example, from Apple - with this there are no problems. But what if our devices have different OS? Separately write applications for each of the OS? It is not comfortable! And where to store the general data? Thus, we can highlight the following list of problems:


Cloud technologies help solve these problems. Let's look at a solution using the example of Azure Mobile Services.

Create an app using Azure Mobile Services

The application that we consider - the list of tasks. This is the simplest application that you can download by logging on to the Azure Portal. In this application, you can create tasks, delete them and rename. The application includes basic functionality for working with Azure services.



We need this application to work on different devices with different operating systems, and that everything is synchronized. Let's look at an example of a task list, how to write a mobile application using Azure Mobile Services using PhoneGap. PhoneGap is a framework that allows you to write code once, and then compile it for multiple platforms. In other words, you do not need to write native applications for each OS.

In addition to PhoneGap, for the development of such an application you will need:


To solve the challenges we face, you need to create an Azure Mobile service. To do this, we need to go to Azure Portal , and click “create mobile service” (compute> mobile service> create):



What is important in the mobile service? Its URL, the database with which it will work (we cannot create a mobile service without it), the region (the place where your data will be placed) and the language in which you will write the service logic. For example, I chose Node.js for this application (you can also choose .NET).





After creating the service, you enter the portal:



Here you see your service. Go to it and see the following "start page":



Here you can choose one of the platforms that you will use to present (UI) your application. That is, the next step you will download is a ready project with all the necessary connected libraries in the desired language, with which you will continue to work.

Of course, you can create an environment in other environments, you can write from scratch, but it will be more convenient to download from here. Here you can download native applications for Android, iOS or Windows, or just an HTML application. Xamarin and PhoneGap are frameworks that allow cross-platform development. They differ in that in Xamarin you write on .NET, and in PhoneGap - in JS; and then you just compile the code and run the application on the platform you need.

Choosing PhoneGap, you see such a small step-by-step instruction:



Here are the necessary tools. In the second step, you must create a table for the base application — this will be the table in the database associated with your mobile service. Then you download the application and can use it. Everything!

Some theory

And now I will talk about how this all works.

Any such application that works with data will have the following structure:



Of course, your application may be more complicated, but in any case it includes these parts: the view (the only thing that will be placed on the device), the database (which the application works with) and the server logic (that will transfer data from the database to the view) . The diagram shows that Azure is responsible for the database and server logic. The server logic you have originally written to Azure — you can only modify it a little to suit your needs.



What else does Azure provide besides data storage and logic?



Database

As for the database, it is a SQL database. You can write any queries to it, and there is indexing that you need.

A very nice thing is a dynamic scheme that allows you not to build all the tables completely. Suppose your application has four columns in a table, and there you entered some more data for which columns are not provided. Do not worry - everything will be added!

The database can be easily managed from all possible portals and applications designed for this. These can be Azure Portal (read and delete), SQL Portal (Silverlight), SQL Management Studio (Windows) and the REST API.

REST API

REST API is attached not only to work with the database, but also to the service in general. This is the only mechanism for interacting with Azure services that our smartphone has. All requests will go via HTTP (although, I must say, mobile services are already emerging that allow working offline). Requests will look like this:



The base address consists of the following elements: [your service name] .azure-mobile.net / tables / [table name].

Todoitem speakers

A little more talk about the database.

In any table there are columns “id”, “created”, “updated”, “version” and “deleted”. These columns, with the exception of "id", we do not get from the default database when we get the item. They are used to determine the time of creation, etc. The main thing is to remember that they are, and not to create something extra. There are also two columns (“text” and “complete”), created using a dynamic scheme.



And here is an example of filling the table (what type of data is used in each column, you can see in the image above):




Column "_deleted" - the implementation of the mechanism of "soft deletion." That is, when you delete a record, you delete it imaginary: it remains in the table, but users will not see it, and if you need it, you can quickly restore it. If you still need to completely delete the record under the delete action, you can write your own implementation for this - it is written very simply. Alternatively, you can delete the entry manually if it is at some stage of testing.

Server logic

For mobile services, two server logic languages ​​are supported: .NET Web API and Node.js.



Node.js provides customized SQL tables by default. Requests to tables are hidden - we do not write the request itself.

The .NET Web API provides all the features that exist when developing common server applications. You can even connect NoSQL-databases: MongoDB and Table Storage.

Implementation example

The most convenient thing about mobile services is that you don't need any environment, no environment to write server-side logic. Here, for example, a screenshot from the portal:



You choose a table, select the “script” tab and see ready-made scripts for four operations: insert, update, delete and receive. By default, the script is reduced to “request.execute”. That is, we get our item, the user who performs these actions, and the object that is responsible for this request. Here the logic for validation is written: it happens already here, and if it does not pass, we send a special object “statusCodes” and a certain message, but do not execute “execute”.

And so I wrote this code on the portal, saved it, and my application that works via the Internet (I did not recompile it and did not change it) will work on the basis of this new logic.

What else can be done with the cloud service?

Let's look again at the “start page” of our service:



There is a tab “Data” - work with a database. There you will see a list of tables and you can create new ones. In the table itself, you will see the data that it contains. And in the “Script” section you will see what operations there are to work with the table now.

The next tab is “API”. We can write our own API, if we are not satisfied with the simple functions of creating, updating, and deleting, if we have, for example, some more complex interaction between several tables.

There is a “Scheduler” - a task scheduler.

“Push” - create notifications.

“Identity” - you can configure the authentication mechanism in the application. All identification data will be in the “user” object that you saw in the screenshot.

“Scale” - service scaling options: you can change the amount of resources allocated for servicing your services, adjust the number of push notifications, etc.

As for languages, now you can write applications on .NET, Node.js, Java, PHP and Python, host them in Azure and interact with all the services provided.

Azure, of course, is not free, but for students there is a program DreamSpark, which provides many features for free. There is also a trial subscription for the month.

In general, working with Azure Mobile Services is very convenient and simple: all changes are made on the fly (at least in the case of Node.js) and do not need to be recompiled. The code can be quickly changed from any device. This is ideal for simple applications and prototypes. But for complex projects it is hardly suitable - one day we will encounter restrictions that will be quite difficult to get around.

Finally, I want to note that since writing the article, Microsoft Azure managed to introduce a new service, Azure Mobile Apps, which is intended in the future to replace Azure Mobile Services. But this is the topic of the next article.

Source: https://habr.com/ru/post/276031/


All Articles