Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Extension #6

Open
luizfbicalho opened this issue Jul 11, 2019 · 11 comments
Open

Async Extension #6

luizfbicalho opened this issue Jul 11, 2019 · 11 comments

Comments

@luizfbicalho
Copy link

Have you ever consider to make ?

.ToDataSourceResultAsync(request)

@linmasaki
Copy link
Owner

Maybe I will add this feature in the future, but I think is not necessary now. Thank for your advice 😃

@luizfbicalho
Copy link
Author

If I could help in this.

@luizfbicalho
Copy link
Author

`

 public static async Task<DataSourceResult> ToDataSourceResultAsync<T>(this 
 Task<IQueryable<T>> queryable, int take, int skip, IEnumerable<Sort> sort, Filter filter, 
 IEnumerable<Aggregator> aggregates, IEnumerable<Group> group)
    {
        var waitedQueryable = await queryable;
        var errors = new List<object>();

        // Filter the data first
        waitedQueryable = Filter(waitedQueryable, filter, errors);

        // Calculate the total number of records (needed for paging)            
        var total = await waitedQueryable.CountAsync();

        // Calculate the aggregates
        var aggregate = Aggregates(waitedQueryable, aggregates);

        if (group != null && group.Any())
        {
            //if(sort == null) sort = GetDefaultSort(queryable.ElementType, sort);
            if (sort == null) sort = new List<Sort>();

            foreach (var source in group.Reverse())
            {
                sort = sort.Append(new Sort
                {
                    Field = source.Field,
                    Dir = source.Dir
                });
            }
        }

        // Sort the data
        waitedQueryable = Sort(waitedQueryable, sort);

        // Finally page the data
        if (take > 0)
        {
            waitedQueryable = Page(waitedQueryable, take, skip);
        }

        var result = new DataSourceResult
        {
            Total = total,
            Aggregates = aggregate
        };

        // Group By
        if ((group != null) && group.Any())
        {
            //result.Groups = queryable.ToList().GroupByMany(group);                
            result.Groups = waitedQueryable.GroupByMany(group);
        }
        else
        {
            result.Data = await waitedQueryable.ToListAsync();
        }

        // Set errors if any
        if (errors.Any())
        {
            result.Errors = errors;
        }

        return result;
    }

`

@linmasaki
Copy link
Owner

Thank you for your idea, I will check it later. 😃

@luizfbicalho
Copy link
Author

How about this?

@linmasaki
Copy link
Owner

How about this?

I add it to version 3.1.1. You can give me some feedback or recommendation if it is convenient. Thanks 😊

@luizfbicalho
Copy link
Author

Nice, thanks

I expected the async code to use the ToListAsync from EFCore
maybe receive the cancellation token

@linmasaki
Copy link
Owner

Uhhh...In my opinion, I want to keep this library simple and not depend too more on an external library.

P.S. I thought that .Net Core 2.2 may be able to use the way you suggested.

@luizfbicalho
Copy link
Author

I understand, you're right not to put any mode dependencies.
maybe a async method that could receive an action to generate the ToList, this way I could create another extension and pass the action s=>s.ToListAsync() on the ToDataSourceAsync()

@aaronasmith
Copy link

The current implementation uses Task.Run which will just spawn a threadpool thread. This is detrimental to some applications.

@luizfbicalho
Copy link
Author

I think that this project isn't alive anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants