concept parallel in category entity framework

appears as: parallel
Entity Framework Core in Action, SE

This is an excerpt from Manning's book Entity Framework Core in Action, SE.

Async/await is a feature that allows a developer to easily use asynchronous programming, running tasks in parallel. Up to this point in this book I have not used async/await because I hadn’t explained this feature. But you need to know that in real applications that have multiple requests happening at the same time, like ASP.NET Core, most of your database commands will use async/await.

Parallel tasks are useful in various scenarios. Say you’re accessing multiple, external sources that you need to wait for before they return a result. By using multiple tasks running in parallel, you gain performance improvements. In another scenario, you might have a long-running task, such as processing an order fulfillment in the background. You use parallel tasks to avoid blocking the normal flow and making your website look slow and unresponsive. Figure 5.10 shows a background task example where a long-running process is run on another thread so that the user isn’t held up.

Figure 5.10 Moving long-running processes to a background task that runs in parallel to the main website, which makes the website feel more responsive. In this example I use an ASP.NET Core backgroundService to run the long-running task. When the task is finished, it uses SignalR to update the user’s screen with a message saying that the long-running task has finished successfully (SignalR is a library that allows a ASP.NET Core app to send messages to the user’s screen).
Entity Framework Core in Action

This is an excerpt from Manning's book Entity Framework Core in Action.

Async/await is a feature that allows a developer to easily use asynchronous programming, running tasks in parallel. Async/await is a big topic, but in this section, you’ll look only at how using async/await can benefit an ASP.NET Core’s application scalability. It does this by releasing resources while waiting for the database server to carry out the command(s) that EF Core has asked it to do.

Figure 5.9 Moving long-running processes to a background task that runs in parallel to the main website. This makes the website feel more responsive.

c05_09.png

I’m using the xUnit unit test library (see https://xunit.github.io/) because Microsoft supports it well. xUnit is also quicker than some other unit test frameworks, such as NUnit (which I used to use), because xUnit can run unit test classes in parallel. This has downsides, which I show you how to get around, but it does mean you can run your complete unit test suite a lot quicker.

Listing 15.21 Capturing EF Core’s logging and outputting it to the unit test console
private readonly ITestOutputHelper _output;    #1  
public TestEfLogging(ITestOutputHelper output)    #2  
{
    _output = output;
}

[Fact]
public void TestEfCoreLoggingExample()
{
    //SETUP
    var options = SQLiteInMemory
        .CreateOptions<EfCoreContext>();
    using (var context = new EfCoreContext(options))
    {
        context.Database.EnsureCreated();
        context.SeedDatabaseFourBooks();
        var logs = context.SetupLogging();    #3  

        //ATTEMPT
        var books = context.Books.ToList();    #4  

        //VERIFY
        foreach (var log in logs.ToList())    #5  
        {                                     
            _output.WriteLine(log.ToString());  #6  
        }                                     
    }
}

#1   In xUnit, which runs in parallel, you need to use the ITestOutputHelper to output to the unit test runner.
#2   The ITestOutputHelper is injected by the xUnit test runner.
#3   Sets up the logging, which returns a reference to a list of LogOutput classes. This contains separate properties for the LogLevel, EventId, Message, and so on.
#4   The query that you want to log
#5   Adds a ToList method on the end of the logs. This stops the unit test from failing if there’s bleed from another unit test running in parallel
#6   Outputs each log to the unit test runner window