Print Page | Close Window

IPager<T>

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4605
Printed Date: 10-Aug-2020 at 7:39pm


Topic: IPager<T>
Posted By: cypher
Subject: IPager<T>
Date Posted: 20-Nov-2013 at 7:31am
We have started to Implement the IPagerRepository<T> with Cocktail.

The Implementation is straight forward with the help of you'r Documentation.

But we are really not happy, that we have to Query the complete last Page, in order to have the TotalAmount initalized in The 
IPager<T>

We have traced the Querys on SQL Profiler, and found out, that we have to fire three Querys 

1.(Count) 
2. LastPage
3. FirstPage

To initialize the IPager<T> to show the Pager.TotalDataSourceItemCount.

This is really sad, because we would like to query the database for the cound, if the first query starts.

Is there a Way for you to implement the IPager<T> from Cocktail, to have it initialize (Count) if the Pager.TotalDataSourceItemCount == -1 without loading the last page?

We really want to avoid, loading the last page, just to initialize the pager (get the TotalSourceItem)




Replies:
Posted By: mgood
Date Posted: 20-Nov-2013 at 12:58pm
Unfortunately, there is not. EF doesn't provide an inline count. You either have to run a count query or fetch the last page to know how many total records there are. That's just how it is. I recommend not to use the Total properties from IPager<> and instead just make a call to CountAsync() to get the total count and use that to calculate the number of pages etc. 


Posted By: cypher
Date Posted: 21-Nov-2013 at 6:38am
Thank you for you answer.

We have stripped out the generation of Predicates (Filters on an Entity) and pass it now into a SearchService, that has a Count And IPager Method.


   /// <summary>
    ///     Predicate to filter the entities.
    /// </summary>
    /// <typeparam name="T">the Model/Projection to apply the filter on</typeparam>
    public interface IPredicate<T> : where T : IEntity
    {
        #region Public Methods

        /// <summary>
        ///     Applies the filter.
        /// </summary>
        /// <returns></returns>
        Expression<Func<T, bool>> ApplyFilter(Expression<Func<T, bool>> filters);

        #endregion
    }

Here is a sample implementation:

 public class CustomerFreeTextPredicate : IPredicate<Customer>
    {
        #region Fields

        private readonly string _searchTerm;

        #endregion

        #region Constructors and Destructors

        public CustomerFreeTextPredicate(string searchTerm)
        {
            _searchTerm = searchTerm;
          //  DisplayName = string.Format(Resources.CustomerFreeTextPredicate, _searchTerm);
        }

        #endregion

        #region IPredicate<Customer> Members

        public string DisplayName { get; set; }

        public Expression<Func<Customer, bool>> ApplyFilter(Expression<Func<Customer, bool>> filter)
        {
            return
                filter.And(
                    x =>
                    (x.CustomerName.Contains(_searchTerm) || x.CustomerNumber.Contains(_searchTerm)) &&
                    x.LegalEntityRefs.Any(
                        ler =>
                        ler.LegalEntityID.Equals(VisUser.LegalEntityId) && (ler.IsDeleted.HasValue && ler.IsDeleted.Value == false) &&
                        ler.IsActive.Equals(true)));
        }

        #endregion
    }


That all works, due to you fantastic implementaion of IRepository<T> !



Print Page | Close Window