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
has a collection of Author
s (Many-to-Many)Author
has a Collection of Book
s (Many-to-Many)Series
has two collections; Books
and Authors
Book
and Author
have a Series
collectionMy 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:
OnSaving
eventAfterConstruction
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.
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.