New Posts New Posts RSS Feed: IPager<T>
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

IPager<T>

 Post Reply Post Reply
Author
cypher View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Nov-2012
Location: Deutschland
Posts: 20
Post Options Post Options   Quote cypher Quote  Post ReplyReply Direct Link To This Post Topic: IPager<T>
    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)



Edited by cypher - 20-Nov-2013 at 7:36am
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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. 
Back to Top
cypher View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Nov-2012
Location: Deutschland
Posts: 20
Post Options Post Options   Quote cypher Quote  Post ReplyReply Direct Link To This Post 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> !
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down