Lenses
Overview
Lenses are specialized reports that you define using a simple and easy configuration. It's a way to gain detailed insights into your Entity Framework Core data.
You can view projections of your data by defining how you want to see your data.
Coravel Pro will do the rest:
- Apply pagination at the database level automatically
- Display your data in a beautiful tabular formatted report
Registering Your Lenses
You do not have to register your lenses - Coravel Pro will scan your assemblies for you and rig them up auto-magically! Just create your lenses and they will appear in the menu for you.
How To Use Lenses
Given the menu above, you might create the following lenses to view:
- Users in your system
- Blog posts ordered by publish date
- An aggregation of your site's daily traffic for the current month
- The top X most popular posts on your blog
Creating A Lens
Let's imagine we were doing the second item in the list above.
Creating a lense is super easy.
Create a class in your solution that implements the interface Coravel.Pro.Features.Lenses.Interfaces.ILense
.
You'll have two items to fill-out:
string Name
: The name that appears in the menu.IQueryable<object> Select(string filter)
: The projection of your entity data you want displayed. Thefilter
parameter refers to the value from the search box on the UI.
For example, to view your blog posts ordered by the created date you might create this lense:
public class BlogPosts : ILense
{
private ApplicationDbContext _db;
public BlogPosts(ApplicationDbContext db)
{
this._db = db;
}
// This is the text displayed on the menu.
public string Name => "Blog Posts";
// This returns the projection for Coravel Pro to display.
public IQueryable<object> Select(string filter) =>
this._db.BlogPosts
.Select(i => i)
.Where(i =>
filter == null
? true
: i.Title.Contains(filter)
)
.OrderByDescending(i => i.CreatedAt);
}
Important
You must return an IQueryable<object>
which means you can Select
, OrderBy
, Where
, etc. on your data. But do not fetch it. Coravel Pro will handle that part for you 😉.
About Projections
Selecting
You may project anything you want. For example, you may want to perform some aggregation across the past month of your data.
If your entity does have many properties then it's useful to project a subset of your entity's data:
.Select(i => new { i.Title, i.Url })
Filtering
The Select
method of your lense will be given a filter
parameter that corresponds to the value in the search box on the UI.
This value is null
when empty. Otherwise it will be what is typed in the search box.
You may filter as you please - across multiple properties, one or just ignore it.
The lense class sample above is a typical pattern for filtering your data.
FYI
As pagination occurs at the database level it will take the filter into account when displaying page numbers.
Ordering
You may call OrderBy
, OrderByDescending
, etc. to order the results of your lenses.