1
\$\begingroup\$

I have a tracking software. At first I just send the position to the web Service

[Table("Positions")]
public class SalesmanPosition
{
    [PrimaryKey, AutoIncrement]
    public int PositionId { get; set; }
    public int SalesmanId { get; set; }
    public double X_long { get; set; }
    public double Y_lat { get; set; }
    public DateTime Event_time { get; set; }
}

private void PositionSaveWeb(SalesmanPosition sp)
{
    var httpWebRequest = (HttpWebRequest)WebRequest
                           .Create("http://myserver.com/ApiTest/api/salesmanPosition");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 
            | SecurityProtocolType.Tls11 
            | SecurityProtocolType.Tls;

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = JsonConvert.SerializeObject(sp);
        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();
    }

    string result = TryWebRequest(httpWebRequest);
}

But then realize sometimes internet connection isn't available so I decide store the position on a sqlite db until the connection is available again. So I create another class to handle the db.

public static class OrmExample
    {
        private static string dbPath = "/storage/emulated/0/DCIM/myDatabase.db3";

        public static void StartDatabase()
        {
            SQLiteConnection db = new SQLiteConnection( dbPath, false);
            db.CreateTable<SalesmanPosition>();
        }

        public static List<SalesmanPosition> GetPendingPositions()
        {
            List<SalesmanPosition> query;
            using (SQLiteConnection db = new SQLiteConnection(dbPath, false))
            {
                query = db.Query<SalesmanPosition>("SELECT * FROM [Positions]");
            }                    
            return query;
        }

        public static void PositionDelete((SalesmanPosition sp))
        {
            using (SQLiteConnection db = new SQLiteConnection(dbPath, false))
            {
                db.Delete(sp);
            }    
        }

        public static void PositionInsert(SalesmanPosition sp)
        {            
            using (SQLiteConnection db = new SQLiteConnection(dbPath, false))
            {
                db.Insert(sp);
            }            
        }
  }

Right now In my main code I have

private void SavePosition ( SalesmanPosition newPosition ) {

    GetConnectionStatus();

    if (isOnline)
    {                
        PositionSaveWeb(newPosition);

        foreach( SalesmanPosition sp in OrmExample.GetPendingPositions() {        
           PositionSaveWeb(sp);
           PositionDelete(sp);
        }
    }
    else
    {             
        OrmExample.PositionInsert(newPosition);
    }  
}

But not sure I should add all those function as method for the class SalesmanPosition

  • SavePosition
  • PositionInsert
  • PositionDelete

And on the Orm only the StartDatabase and GetPendingPositions()

So should be resume to

 newPosition.Save(); // include connection check and decide if send web or save locally.
 if (isOnline)
 {                
    foreach( SalesmanPosition sp in OrmExample.GetPendingPositions() {        
       sp.Save();
       sp.Delete();
    }
 }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.