0
votes

I have a association in my poco class, ex:

public class Category() {
    [Key]
    public int id { get; set}
    public string Name { get; set; }
    /* HERE */
    public virtual ICollection<Book> Books {get; set;}
}

public class Book() {
    [Key]
    public int id { get; set}
    public string Name { get; set; }
}

I use MVVM patern, MVVM Light and RIA Services Toolkit. My Domain Service implementation contains a method GetCategories that include their books, ex:

public IQueriable<Category> GetCategories()
{
    return Model.Categories.Include("Books").OrderBy(pCategory => pCategory.Name);
}

In my ViewModel I have a DomainCollectionView that load GetGruposQuery. I also have a property for bind a grid and other controls, like:

public ICollectionView CollectionViewCategories {
    get { return myDomainCollectionViewCategories;}
}

I need get a child property CollectionView.Books for bind my controls and ADD, REMOVE itens in view, but this property is only EntityCollection and isn't a DomainCollectionView that contains methods for ADD, REMOVE, etc.

How I can get the current Books property (of CollectionViewCategories) as DomainCollectionView in my ViewModel?

Thank you!


I solve this question with: (CollectionViewCategories.CurrentItem as Category).Books.Remove(CollectionViewBooks.CurrentItem as Book)

private ICollectionView CreateView(Object source)
{
    CollectionViewSource cvs = new CollectionViewSource();
    cvs.Source = source;
    return cvs.View;            
}
//...
//After CollectionViewCategories loaded:
CollectionViewCategories.CurrentChanged += (s, e) =>
{
    if (CollectionViewCategories.CurrentItem != null)     
    {
        CollectionViewBooks = CreateView(fContext.Categories.Where(p => p.Id == (CollectionViewCategories.CurrentItem as Category).Id).FirstOrDefault().Books);
    }
    else
    {
        CollectionViewBooks = null;
    }
    RaisePropertyChanged("CollectionViewBooks");
};  
1

1 Answers

0
votes

Why do you need it ? EntityCollection do have these methods (Add,Remove...)
If you obtain an error while invoking them, it could lead to the fact that the Book class is not fully exposed to the client (lack of insert/update/delete methods on the domainservice) Just to clarify, DomainCollectionView is inteded to wrap an EntityCollection primarily for better dealing with MVVM and binding in general (see this link)