Skip to content
ahanusa edited this page Oct 20, 2015 · 16 revisions

Peasy was designed to be thread safe, so that you can safely execute many [commands] via threads without worrying about race conditions over resources.

There will be many times when you want to perform many updates against a data store all at once. For example, let's take a scenario where a user is making modifications to a grid, and modifying the underlying bound objects. Further, assume that all updates are performed once the user clicks a save button.

In a typical scenario, the application might loop through each object that is dirty and perform an update on it, in a synchronous manner. This can take alot of time, and can often times be advantageous to spawn threads (or System.Threading.Task) to speed up this operation.

Here's an example that illustrates this scenario:

var ids = new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var categories = ids.Select(id => _categoriesService.GetByIDCommand(id).Execute().Value);
categories.ForEach(c => c.LastModifiedDatetime = DateTime.Now);
Parallel.ForEach(categories, category =>
{
    _categoriesService.UpdateCommand(category).Execute();
});

In this example, we synchronously load all categories by ID, specified in the ids array. We then modify the LastModifiedDatetime property on each object. Then, to speed up the updating operations, we invoke the UpdateCommand method of the categories service within a Parallel.ForEach, which essentially spawns a new thread context for each update command to execute within.

In this scenario, we have exponentially decreased the time to update the dirty category objects in a completely thread-safe manner.

Clone this wiki locally