4

I've been experimenting with C# and DevExpress and came across a situation for which I cannot find a simple solution.

I've got three objects:

  • Book
  • Author
  • Series

 

  1. A Book has a collection of Authors (Many-to-Many)
  2. An Author has a Collection of Books (Many-to-Many)
  3. A Series has two collections; Books and Authors
  4. Both Book and Author have a Series collection

My problem is that whenever I add a Book to a Series, the Author(s) of that Book should also be added to the Series.

[Association("SeriesBooks")]
public XPCollection<Book> Books => GetCollection<Book>(nameof(Books));

[Association("SeriesAuthors")]
public XPCollection<Author> Authors
{
    get { return GetCollection<Author>("Authors"); }
}

I've considered the following:

  • Use the OnSaving event
  • Adding a setter to my associations
  • Using the AfterConstruction

But since I'm a beginner, I haven't been able to actually get anything out of this.

I'd be glad with all the help you can offer.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
Peter
  • 8,776
  • 6
  • 62
  • 95

1 Answers1

3

Don't have any experience with DevExpress, but if you really wanted a prop for the authors in the series class I would create a read-only prop that reads the Authors from the list of the books. Something like:

public List<Author> Authors { 
        get {
            Books.SelectMany(x => x.Authors).ToList();
        } 
    }

It would eliminate the work involved in mainatining a seperated list of Authors. Any change to any of the books included in the series, including addition and removal of books themselves, or any changes to a books authors, would automatically be reflected in the Series without any other code.

Kamal
  • 164
  • 2
  • what about that https://stackoverflow.com/questions/66647272/threadsafedatalayer-and-idatalayer-devexpress-xpo-cant-map-classinfo-to-same-ta can you answer it please –  Mar 21 '21 at 02:49