Simple Asynchronous List #2337
Unanswered
CrossSlide
asked this question in
Questions
Replies: 1 comment 1 reply
-
This is a modern/current approach to paged loading of a list. using System;
using Csla;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var loader = new BigListLoader();
BigList bigList = DataPortal.Create<BigList>();
loader.StartLoading();
loader.PageLoaded += (list) =>
{
Console.WriteLine($"NEW PAGE RECIEVED");
bigList.AddRange(list);
foreach (var item in bigList)
Console.WriteLine($"Id {item.Id}, Page {item.Page}");
};
Console.ReadLine();
}
/// <summary>
/// Loads a BigList by pages of 10 items each
/// </summary>
public class BigListLoader
{
/// <summary>
/// Event raised when a new page of items
/// is available. Add this page of items
/// to your existing BigList object.
/// </summary>
public event Action<BigList> PageLoaded;
private int page;
const int pageSize = 10;
/// <summary>
/// Start the async load process by pages.
/// Each page is returned via the PageLoaded
/// event.
/// </summary>
public async void StartLoading()
{
BigList result;
do
{
page++;
result = await DataPortal.FetchAsync<BigList>(page, pageSize);
if (result.Count > 0)
{
PageLoaded?.Invoke(result);
}
} while (result.Count > 0);
}
}
/// <summary>
/// BigList business list
/// </summary>
[Serializable]
public class BigList : BusinessListBase<BigList, Item>
{
/// <summary>
/// Create an empty list
/// </summary>
[Create]
[RunLocal]
private void Create()
{ }
/// <summary>
/// Standard fetch of all items
/// </summary>
[Fetch]
private void FetchAll()
{
using (LoadListMode)
{
for (int i = 0; i < 30; i++)
{
Add(DataPortal.FetchChild<Item>(i + 1, 1));
}
}
}
/// <summary>
/// Fetch a single page of items
/// </summary>
/// <param name="page"></param>
/// <param name="pageSize"></param>
[Fetch]
private void FetchPage(int page, int pageSize)
{
var start = (page - 1) * pageSize;
if (start < 30)
{
using (LoadListMode)
{
for (int i = start; i <= start + pageSize; i++)
{
Add(DataPortal.FetchChild<Item>(i, page));
}
}
}
}
}
/// <summary>
/// Item class defining children of BigList
/// </summary>
[Serializable]
public class Item : BusinessBase<Item>
{
public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(nameof(Id));
public int Id
{
get => GetProperty(IdProperty);
private set => LoadProperty(IdProperty, value);
}
public static readonly PropertyInfo<int> PageProperty = RegisterProperty<int>(nameof(Page));
public int Page
{
get => GetProperty(PageProperty);
private set => LoadProperty(PageProperty, value);
}
/// <summary>
/// Fetch a single child object
/// </summary>
/// <param name="id"></param>
/// <param name="page"></param>
[FetchChild]
private void FetchChild(int id, int page)
{
using (BypassPropertyChecks)
{
Id = id;
Page = page;
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to create a BO List that fetches asynchronously to speed up my WPF UI. I've never tried asynchronous type BO's and don't understand how to call async DataPortal_Create(handler). MyList = BigList.GetBigList(handler?). I guess I'm not sure of the "handler" part. How do I do this? Thanks for the help.
(Note: csla v4.6)
MY NORMAL EXAMPLE:
Beta Was this translation helpful? Give feedback.
All reactions