Chapter 4. Data – the Monkeys Catalog

We have started this book sitting in a virtual airplane that is now flying us over Xamarin Mobile Multiplatform Development. It's time to look down a little bit and view the map of the land that we are going to visit every time we make a new app.

One common thing about each app that we develop is that, at some point, we will need to write and read data.

Unfortunately, when we work with mobile phones we cannot trust the network and we cannot have a full-time communication with a central SQL server. This means that we need to find a way to save data locally and then synchronize it with the server as soon as we've got connectivity.

In this chapter, we will explore how to design and develop an architecture that allows us to work with and manage local data.

Mobile architectural pattern

When we start to design a new app we should group the core functionality in logical components. These logical groupings are called layers. Layers help to differentiate between the different kinds of task performed by the components, making the reusability and maintenance of our code easier. Each logical layer contains a number of discrete component types grouped into sub-layers, with each sub-layer performing a specific type of task.

Having an application divided into separate layers, each one of them has distinct roles and functionality that help us with the following:

  • Maximizing maintainability of the code
  • Answering "Where should I create this file?" or "Where should I install this package?"
  • Optimizing the way that the application works when deployed in different ways and sharing most of the backend logic between platforms

Presentation, business, and data layers

We can imagine our app as a set of cooperating components grouped into layers.

A simplified representation of how we need to structure our app shows the high-level layers and their relationships with users and other applications and services:

Presentation, business, and data layers

This is a basic principle in software development and usually it is a good practice to create a different library (a new project) for each of those layers. When we develop mobile applications we need to balance the number of libraries with the limited performance of a mobile device.

In order to have a nice structure of our code, well balanced with the performances, we can create folders inside our project instead of multiple projects.

We can start creating a folder called Base and a folder called Core inside the main Xamarin Form project.

We are going to explore how to design and create our objects and functionality to manage the core of the entire app. Let's have a quick overview of the layers that we are going to create as folders and the meaning we will give to them:

  • Presentation Layer: In this folder, we will insert the User-Oriented functionality responsible for managing user interaction with our app. Those components provide a bridge into the core business logic that we will create on the business layer.
  • Business Layer: In this folder, we will implement the core functionality of our system.

    The business logic is the way we will have to call one or more data components or services interfaces. It should be the only access point for the presentation layer to our core.

  • Data Layer: Here we will implement the access to the data that is embedded in our system or exposed from other networked systems. The business layer consumes the objects in the data layer.

    It would be very useful to build our helpers in order to minimize the code to write, in order to make our data component.

  • Services: A service is a way to communicate with other systems by passing messages. It can be considered from our app as a component, but in reality, if we explore it, we will discover that it is a software component like any other, a system that in general can be logically grouped into presentation, business, and data layers, like any other application.

    Our app can make use of the services without being aware of the way they are implemented, but accessing them using their presentation layer, just like our final user will be able to use our app without being aware of this book and our architecture strategies.

  • Service Layer: This is an optional layer that we might consider to add to our app when we decide to provide services to other apps.

    In this case, we can expose the business functionality as services providing a new channel to access the application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.161.161