0

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?

4
  • 1
    While 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. Commented Feb 13 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. Commented Feb 13 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. Commented Feb 13 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. Commented Feb 13 at 21:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.