Search code examples
silverlightrepository-patternwcf-ria-services

DomainCollectionView with DomainService + Repository pattern


i have a view model in silverlight which loads contacts i.e.:

_ContactsSource = new EntityList<ContactItem>(_ContactsDomainContext.ContactItems);
_ContactsLoader = new DomainCollectionViewLoader<ContactItem>(LoadCurrentCategoryContacts, LoadContactsCompleted);
_ContactsView = new DomainCollectionView<ContactItem>(_ContactsLoader, _ContactsSource);

    public LoadOperation<ContactItem> LoadCurrentCategoryContacts()
    {
        var query = _ContactsDomainContext.GetContactsByCategoryIdQuery(CurrentContactCategory.Id);
        query.IncludeTotalCount = true;

        var op = _ContactsDomainContext.Load(query.SortAndPageBy(_ContactsView));
        return op;
    }

    public void LoadContactsCompleted(LoadOperation<ContactItem> op)
    {
        if (op.HasError)
            op.MarkErrorAsHandled();
        else if (!op.IsCanceled)
        {
            _ContactsSource.Source = op.Entities;

            if (op.TotalEntityCount > -1)
                _ContactsView.SetTotalItemCount(op.TotalEntityCount);
        }
    }

and a domain service with method which returns contacts DTO, based on their category:

   public IQueryable<ContactItem> GetContactsByCategoryId(int categoryId)
    {
        List<ContactItem> result = new List<ContactItem>();
        Mapper.Map(_contactRepository.GetAll(x => x.ContactCategoryId == categoryId), result); //automapper
        return result.AsQueryable();
    }

the question is: every time the domain service is used, it gets all table rows from database (i.e. _ContactsView PageSize is set to 10, the result in "return result.AsQueryable();" will have 140 elements), but only 10 elements is shown.

What is the best way to use DomainCollectionView with wcf ria service + repository pattern, so all the filtering and paging will happen on server side, and only necessary amount of data will be returned to silverlight?

Thank You !


Solution

  • The paging is still occurring on the server side, but it happens at the framework level. You're still only pulling 10 records to the client. What you've noticed, however, is that you're pulling 140 records to the middle (web) tier. This is problematic, but likely not to the same degree as pulling them all to the client.

    The easiest way to fix this with your repository is to pass the pageIndex and pageSize as parameters to your query method. Instead of calling SoryAndPageBy on the client, just call SortBy and pass the view.PageIndex and view.PageSize values through the query. Also, don't forget to Include the TotalEntityCount request in your query.