Greetings,
I need a mobile application that obtains data from an API Service to work also offline, for this I must create a local table that stores the model with the information that I bring from the API when there is an internet connection, the model is the following :
public class Country
{
[PrimaryKey, AutoIncrement]
public int CountryId { get; set; }
public string name { get; set; }
public List<string> topLevelDomain { get; set; }
public string alpha2Code { get; set; }
public string alpha3Code { get; set; }
public List<string> callingCodes { get; set; }
public string capital { get; set; }
public List<string> altSpellings { get; set; }
public string region { get; set; }
public string subregion { get; set; }
public long population { get; set; }
public List<double> latlng { get; set; }
public string demonym { get; set; }
public double? area { get; set; }
public List<string> timezones { get; set; }
public List<string> borders { get; set; }
public string nativeName { get; set; }
public string numericCode { get; set; }
public List<Currency> currencies { get; set; }
public List<Language> languages { get; set; }
public Translations translations { get; set; }
public string flag { get; set; }
public List<RegionalBloc> regionalBlocs { get; set; }
public override int GetHashCode()
{
return CountryId;
}
}
To insert, update and delete records I use the following class:
public class DataService
{
public bool DeleteAll<T>() where T : class
{
try
{
using (var da = new DataAccess())
{
var oldRecords = da.GetList<T>(false);
foreach (var oldRecord in oldRecords)
{
da.Delete(oldRecord);
}
}
return true;
}
catch (Exception ex)
{
ex.ToString();
return false;
}
}
public T DeleteAllAndInsert<T>(T model) where T : class
{
try
{
using (var da = new DataAccess())
{
var oldRecords = da.GetList<T>(false);
foreach (var oldRecord in oldRecords)
{
da.Delete(oldRecord);
}
da.Insert(model);
return model;
}
}
catch (Exception ex)
{
ex.ToString();
return model;
}
}
public T InsertOrUpdate<T>(T model) where T : class
{
try
{
using (var da = new DataAccess())
{
var oldRecord = da.Find<T>(model.GetHashCode(), false);
if (oldRecord != null)
{
da.Update(model);
}
else
{
da.Insert(model);
}
return model;
}
}
catch (Exception ex)
{
ex.ToString();
return model;
}
}
public T Insert<T>(T model)
{
using (var da = new DataAccess())
{
da.Insert(model);
return model;
}
}
public T Find<T>(int pk, bool withChildren) where T : class
{
using (var da = new DataAccess())
{
return da.Find<T>(pk, withChildren);
}
}
public T First<T>(bool withChildren) where T : class
{
using (var da = new DataAccess())
{
return da.GetList<T>(withChildren).FirstOrDefault();
}
}
public List<T> Get<T>(bool withChildren) where T : class
{
using (var da = new DataAccess())
{
return da.GetList<T>(withChildren).ToList();
}
}
public void Update<T>(T model)
{
using (var da = new DataAccess())
{
da.Update(model);
}
}
public void Delete<T>(T model)
{
using (var da = new DataAccess())
{
da.Delete(model);
}
}
public void Save<T>(List<T> list) where T : class
{
using (var da = new DataAccess())
{
foreach (var record in list)
{
InsertOrUpdate(record);
}
}
}
}
The problem is that when I try to create local storage with the following code:
var url = Application.Current.Resources["URLAPI"].ToString();
var response = await apiService.GetList<Country>(
url,
"/rest/v2/all");
// Storage data local
countries = (List<Country>)response.Result;
dataService.DeleteAll<Country>();
dataService.Save(countries);
An exception occurs when calling the dataService.DeleteAll and dataService.Save methods because the Country model has properties that are List and one that is a Translations type object, but I can not modify the model because these properties are defined in The API Service from which I receive the information, what can I do to cast these properties on the model or how can I solve this?
Thank you very much for your help.