Exemple #1
0
        public bool SaveData()
        {
            RepositoryDTO dto = new RepositoryDTO();

            lock (ClientLock)
            {
                dto.Clients = ClientManager.GetAll().Select(c => new ClientDTO(c)).ToList();
            }
            lock (ProductLock)
            {
                dto.Products = ProductManager.GetAll().Select(p => new ProductDTO(p)).ToList();
            }
            lock (OrderLock)
            {
                dto.Orders = OrderManager.GetAll().Select(o => new OrderDTO(o)).ToList();
            }
            lock (FileLock)
            {
                DataPathWatcher.EnableRaisingEvents = false;
                bool success = true;
                try
                {
                    // TODO: provide a serialization interface and use it
                    System.IO.File.WriteAllText(FullFilePath, JsonConvert.SerializeObject(dto, Formatting.Indented));
                }
                catch (Exception e)
                {
                    Debug.WriteLine($"{e.Message}\n{e.StackTrace}");
                    success = false;
                }
                DataPathWatcher.EnableRaisingEvents = true;
                return(success);
            }
        }
Exemple #2
0
 public bool LoadData()
 {
     lock (FileLock)
     {
         try
         {
             // TODO: provide a serialization interface and use it
             RepositoryDTO dto = JsonConvert.DeserializeObject <RepositoryDTO>(System.IO.File.ReadAllText(FullFilePath));
             lock (ClientLock)
             {
                 lock (ProductLock)
                 {
                     lock (OrderLock)
                     {
                         ClientManager.ReplaceData(new HashSet <IClient>(dto.Clients.Select(c => c.ToIClient())));
                         OrderManager.ReplaceData(new HashSet <IOrder>(dto.Orders.Select(o => o.ToIOrder())));
                         ProductManager.ReplaceData(new HashSet <IProduct>(dto.Products.Select(p => p.ToIProduct())));
                     }
                 }
             }
             return(true);
         }
         catch (Exception e)
         {
             Debug.WriteLine($"{e.Message}\n{e.StackTrace}");
             return(false);
         }
     }
 }