Creating A Data Storage Mechanism

Introduction

 
The storage is required to store and retrieve Point of Interest data. Data can be stored in two types of services as web services and local storage. Many databases are available to store the data in the system locally like SLite and it is a lightweight transactional database engine. To store the structured data in the text format Json and XML are used.
 

Creating the Entity Class

 
Download and open the Xamarin studio.
 
Select POIApp from the solution pad in the Xamarin studio.
 
Right-click the selected POIApp and select the New File option.
 
Select general in the new file dialog box.
 
Create an empty class by selecting the empty class option.
 
Name it as PointOfInterest and click ok.
 
Set the visibility of the class to the public and the class is created in the POIApp project folder.
 

Creating POI storage Interface

 
Select the project in Xamarin Studio and create the new file by selecting the new file.
 
Select the general in the new file dialog box and then select the Empty interface in the template list.
 
Name it as IOPIDataService and click ok.
 
The IReadOnlyList is used to ensure that POI cannot be added to the cache and it is added or deleted by the CRUD operations.
 
The below example is the interface definition.
  1. public interface IPOIDataService {  
  2.     IReadOnlyList < PointOfInterest > POIs {  
  3.         get;  
  4.     }  
  5.     void RefreshCache();  
  6.     pointOfInterest GetPOI(int id);  
  7.     void savePOI(PointOfInterest poi);  
  8.     void DeletePOI(PointOfInterest poi);  
  9. }   

POI storage services

 
The class is to be created for implementing the POI storage services.
 
Name the class as POIJsonService.
 
The visibility of the class is changed to public and the POIJsonService implement the IPOIDataService interface must be specified.
 
Place the implementation methods in the location and select OK.
 
The below example shows the class and method attributes created for the IPOIDataServce interface.
  1. public class POIJsonService: IPOIDataService {  
  2.     public PIOJsonService() {}  
  3.     #region IPOIDataService implementation  
  4.     public void RefreshCache() {  
  5.         throw new NotImplementedException();  
  6.     }  
  7.     public PointOfInterest GetPOI(int id) {  
  8.         throw new NOTImplementeedException();  
  9.     }  
  10.     public void SavePOI(PointOfInterest poi) {  
  11.         throw new NotImplementedException();  
  12.     }  
  13.     public void DeletePOI(PointOfInterest poi) {  
  14.         throw new NotImplementedException();  
  15.     }  
  16.     public System.Collections.Generic.IReadOnlyList < PointOfInterest > POIs {  
  17.         get {  
  18.             throw new NotImplementedException();  
  19.         }  
  20.     }  
  21.     endregion  
  22. }   

Creating Test Methods

 
The three processes that are required to create the test method are creating the new POI, updating the existing POI, and deleting the existing POI.
 

Creating a POI Test

 
The createPOI method is used to create the POI and Create a new instance of PointOfInterest and fill the attributes.
 
The Id is saved on the newly created POI.
 
The cache must be refreshed to check whether the new POI saved and restored from the storage.
 
The getPOI() method is used to retrieve the POI and the asset class is used to ascertain the POI retrieved.
 

Update POI Test

 
The new instance of PointOfInterest was created and the attributes filled out.
 
Then call the savePOI() in the data service.
 
The ID must be saved on the newly created POI. 
 
Refresh the cache and call the GetPOI method to retrieve the POI and the description property is set to a new value.
 
Again, refresh the cache and call the GetPOI method to retrieve the POI based on the ID that was saved. 
 
The asset class is used to ensure that the POI was retrieved.
 

Delete POI Test

 
Create a new instance of PointOfInterest and fill the attributes.
 
Call the savePOI method on the data service.
 
The ID is saved for the newly created POI.
 
Refresh the cache and call the GetPOI method to retrieve the POI and the DeletePOI method is called to delete the POI file.
 
Again, refresh the cache and call the GetPOI method to retrieve the POI based on the saved ID.