Search code examples
c#devexpressxafxpo

Setting multiple properties at once in an XPO Object


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.


Solution

  • 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.