// Utility function that customises a generic item // Importantly, makes use of generics to reduce code bloat // Takes the abstract EditWindow to actually use (determines the type) private void EditData <T>(EditWindow <T> Wnd) where T : DataModel { // Show the window and store the close result bool?Result = Wnd.ShowDialog(); // If closed succesfully, process the item if (Result.HasValue && Result.Value) { // Acquire the (generic) item T New = Wnd.GetItem(); if (New != null) { // Add the item to the relevant table Type t = typeof(T); using (DataRepository Repo = new DataRepository()) { if (t == typeof(Booking)) { Repo.Bookings.Add((Booking)(object)New); } else if (t == typeof(Class)) { Repo.Classes.Add((Class)(object)New); } else if (t == typeof(Department)) { Repo.Departments.Add((Department)(object)New); } else if (t == typeof(Room)) { Repo.Rooms.Add((Room)(object)New); } else if (t == typeof(Student)) { Repo.Users.Add((Student)(object)New); } else if (t == typeof(Subject)) { Repo.Subjects.Add((Subject)(object)New); } else if (t == typeof(Teacher)) { Repo.Users.Add((Teacher)(object)New); } else if (t == typeof(TimeSlot)) { Repo.Periods.Add((TimeSlot)(object)New); } } } } }