A
A
Alex Vodkin2017-08-15 13:10:58
SQLite
Alex Vodkin, 2017-08-15 13:10:58

How to properly organize work with data (C# Desktop App, Database)?

e047ce3285a94be897ccb0646db5cf93.png
Need help with data management.
Subject: desktop application for a hotel.
The Reservations table contains data for each accommodation. Each placement can consist of one or more days (ResDays). For each day of placement can be added extra. service (AddServices).
In the program, I use collections for this. Very simplified:

class AddServiceItem
{
  string Name;
  Decimal Cost;
}
class ResDayItem
{
  DateTime Date;
  Decimal Cost;
  Collection<AddServiceItem> AddServices;
}
class Reservation
{
  DateTime StartDate;
  DateTime StartDate;
  int Status;
  Collection<ResDayItem> ResDays;
}

There are no problems with creating accommodation - the user selects the number of days, a Reservation instance is created, and collections of days and additional. services are filled in by the user in the program interface. After clicking the "Add location" button, everything is written to the database.
Of course, you need to be able to change all the data. When opening a placement for editing, I again create a Reservation instance, and fill the collections with values ​​​​from the database like this
MyReservation = new Reservation();
//создаю коллекцию дней
MyReservation.ResDays = new Collection<ResDayItem>();
//и для каждого дня размещения с Id == resId
foreach (var Day in db.Table<ResDays>().Where(x => x.ResId == resId))
{	//создаю экземпляр ResDayItem
    ResDayItem DayItem = new ReservationDayItem();
  //и в нем коллекцию доп. услуг
  DayItem.AddServices = new Collection<AddServItem>();
    foreach (var AddService in db.Table<AddServices>().Where(x => x.ResDayId == Day.Id))
    DayItem.AddServices.Add(AddService);
    MyReservation.ResDays.Add(DayItem);
}

The question is: how (in what order) to update the database when changing collections?
I would like everything to be written to the database when the user clicks the "Save" button. If the number of days / additional services remains the same, then it's just an update for all records, but what if the user deleted something? How to find out exactly which elements of the collections have been deleted in order to delete the corresponding records from the database?
Maybe I generally applied the wrong approach with such nesting of collections?
How to properly organize work with data in such a task?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question