예제 #1
0
 /// <summary>
 /// Raises the <see cref="E:Selecting"/> event.
 /// </summary>
 /// <param name="args">The <see cref="NBear.Web.Data.NBearDataSourceEventArgs"/> instance containing the event data.</param>
 internal void OnSelecting(NBearDataSourceSelectingEventArgs args)
 {
     if (Selecting != null)
     {
         Selecting(this, args);
     }
 }
예제 #2
0
        protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            NBearDataSourceSelectingEventArgs selectingArgs = new NBearDataSourceSelectingEventArgs(arguments);

            owner.OnSelecting(selectingArgs);
            arguments = selectingArgs.SelectArguments == null ? DataSourceSelectArguments.Empty : selectingArgs.SelectArguments;
            arguments.RaiseUnsupportedCapabilitiesError(this);
            Gateway gateway = owner.Gateway;
            Type    type    = Util.GetType(this.owner.TypeName);

            string sort = owner.DefaultOrderByExpression;

            if (!string.IsNullOrEmpty(arguments.SortExpression))
            {
                string[] parts = arguments.SortExpression.Split(new char[] { ' ' }, 2);
                string   field = "{" + parts[0] + "}";
                sort = field + (parts.Length >= 2 ? " " + parts[1] : " ASC");
            }

            int page = (arguments.MaximumRows > 0 ? (arguments.StartRowIndex / arguments.MaximumRows) + 1 : 1);

            WhereClip where = string.IsNullOrEmpty(owner.FilterExpression) ? WhereClip.All : new WhereClip(owner.FilterExpression);
            OrderByClip orderBy = string.IsNullOrEmpty(sort) ? OrderByClip.Default : new OrderByClip(sort);
            Array       list;

            if (arguments.MaximumRows > 0)
            {
                object pageSelector = typeof(Gateway).GetMethod("GetPageSelector").MakeGenericMethod(type).Invoke(gateway, new object[] { where, orderBy, arguments.MaximumRows });
                list = (Array)typeof(PageSelector <>).MakeGenericType(type).GetMethod("FindPage").Invoke(pageSelector, new object[] { page });
            }
            else
            {
                list = (Array)GetGatewayMethodInfo("EntityType[] FindArray[EntityType](NBear.Common.WhereClip, NBear.Common.OrderByClip)").MakeGenericMethod(type).Invoke(gateway, new object[] { where, orderBy });
            }
            if (arguments.RetrieveTotalRowCount)
            {
                arguments.TotalRowCount = (int)GetGatewayMethodInfo("Int32 Count[EntityType](NBear.Common.WhereClip)").MakeGenericMethod(type).Invoke(gateway, new object[] { where });
            }
            NBearDataSourceSelectedEventArgs selectedArgs = new NBearDataSourceSelectedEventArgs(arguments, list);

            owner.OnSelected(selectedArgs);
            return(selectedArgs.ResultEntities);
        }