I have an "Enterprise Resource Planning" project. It uses a SQLite database.
With Entity Framewok, I got my entity classes.
For CRUD operations, I modified my ObservableCollection:
public class ArtistCollection : ObservableCollection<Artist>
{
private ChinookdbContext _context;
public ArtistCollection(IEnumerable<Artist> artists, ChinookdbContext context) : base(artists)
{
_context = context;
}
protected override void InsertItem(int index, Artist item)
{
Context.Artists.Add(item);
base.InsertItem(index, item);
Console.WriteLine("InsertItem");
}
protected override void RemoveItem(int index)
{
Context.Artists.Remove(this[index]);
base.RemoveItem(index);
}
public void Save()
{
try
{
Console.WriteLine("Clienti: Saved N° " + _context.SaveChanges());
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
}
public ChinookdbContext Context
{
get
{
return _context;
}
}
}
This is my modified entity:
public partial class Artist : INotifyPropertyChanged, IEditableObject
{
private Artist backup;
private bool inTransaction;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public void BeginEdit()
{
backup = new Artist();
backup.ArtistId = ArtistId;
backup.Name = Name;
inTransaction = true;
Console.WriteLine("- BeginEdit");
}
public void CancelEdit()
{
if (inTransaction)
{
ArtistId = backup.ArtistId;
OnPropertyChanged(nameof(ArtistId));
Name = backup.Name;
OnPropertyChanged(nameof(Name));
backup = null;
inTransaction = false;
Console.WriteLine("- CancelEdit");
}
}
public void EndEdit()
{
if (inTransaction)
{
OnPropertyChanged(nameof(ArtistId));
OnPropertyChanged(nameof(Name));
backup = null;
inTransaction = false;
Console.WriteLine("- EndEdit");
}
}
}
All this for one table.
I would like to be able to proceed in a less verbose way, having to use several tables.
Is there a simpler way?
ASh
35.9k9 gold badges68 silver badges89 bronze badges
-
1While I like the idea of "how can I do this more efficiently", your question will no doubt only spur opinionated responses. Eg: You are mixing presentation, business and database concerns into a highly coupled and fragile design. I’d recommend revising your question to spell out your objectives more clearly and highlight specific problems you are experiencing so that folks can provide a clear non-subjective answer.bryanbcook– bryanbcook2025年02月13日 20:08:16 +00:00Commented Feb 13, 2025 at 20:08
-
Property change notifications originate in the affected object's "properties"; not in the "pipeline". Then you can start thinking in terms of generics and interfaces.Gerry Schmitz– Gerry Schmitz2025年02月13日 20:45:51 +00:00Commented Feb 13, 2025 at 20:45
-
@GerrySchmitz I have a listbox with db records (items) represented. If I change one of these from the UI (CRUD operations), it must propagate to the DB. I need INotifyCollectionChanged and INotifyPropertyChanged.Brummell– Brummell2025年02月13日 21:07:19 +00:00Commented Feb 13, 2025 at 21:07
-
I didn't say you didn't need them; yours are just not localized and is causing redundant code. And "backups" can be accomplished via cloning; a generic method.Gerry Schmitz– Gerry Schmitz2025年02月13日 21:18:45 +00:00Commented Feb 13, 2025 at 21:18
lang-cs