DomainCollectionView 和 WCF RIA 服务中的关联属性

发布于 2024-12-14 08:29:07 字数 1832 浏览 0 评论 0原文

我的 poco 类中有一个关联,例如:

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; }
}

我使用 MVVM 模式、MVVM Light 和 RIA Services Toolkit。我的域服务实现包含一个 GetCategories 方法,其中包含他们的书籍,例如:

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

在我的 ViewModel 中,我有一个加载 GetGruposQuery 的 DomainCollectionView。我还有一个用于绑定网格和其他控件的属性,例如:

public ICollectionView CollectionViewCategories {
    get { return myDomainCollectionViewCategories;}
}

我需要获取一个子属性 CollectionView.Books 来绑定我的控件并在视图中添加、删除 itens,但此属性只是 EntityCollection,而不是包含的 DomainCollectionView ADD、REMOVE 等方法。

如何在 ViewModel 中获取当前 Books 属性(CollectionViewCategories)作为 DomainCollectionView?

谢谢你!


我用以下方法解决这个问题:(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");
};  

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");
};  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2024-12-21 08:29:07

为什么需要它? EntityCollection 确实有这些方法(添加、删除...)
如果在调用它们时出现错误,可能会导致 Book 类未完全暴露给客户端(域服务上缺少插入/更新/删除方法)
澄清一下,DomainCollectionView 旨在包装 EntityCollection 主要是为了更好地处理 MVVM 和一般绑定(请参阅此 链接)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文