MicroLite 2.0 Includes

As we covered in the previous post, MicroLite now supports including additional results in a single query.

The include methods are all exposed via the ISession.Include property and enable following:

  • Many<T>(SqlQuery)
  • Scalar<T>(SqlQuery)
  • Single<T>(object identifier)
  • Single<T>(SqlQuery)

You can call include for multiple things, they will all be loaded in a single database call once either ISession.Single, ISession.Fetch or ISession.Paged is called.

The include methods return a token which can be inspected after the all the results have been loaded.

Many

The Many method is the equivalent of the Fetch method, it should be used to retrieve associated records (e.g. the Invoices for a Customer, or Order Items for an Order).

using (var session = sessionFactory.OpenSession())
{
    // Query to fetch the invoices for the customer.
    var invoicesQuery = new SqlQuery("SELECT * FROM Invoices WHERE CustomerId = @p0", 1792);
        
    // Tell the session to include the invoices.
    var invoices = session.Include.Many<Invoice>(invoicesQuery);
        
    // At this point, invoices will point to an IIncludeMany<Invoice> which will have no values.
        
    // Load the customer.
    var customer = session.Single<Customer>(1792);
        
    // We can now acces the invoices for the customer
    foreach (var invoice in invoices.Values)
    {
        // ...
    }
}

Scalar

The Scalar method allows a single value to be included such as the count of invoices for a customer.

using (var session = sessionFactory.OpenSession())
{
    // Query to count the invoices for the customer.
    var invoicesCountQuery = new SqlQuery("SELECT COUNT(InvoiceId) FROM Invoices WHERE CustomerId = @p0", 1792);

    // Tell the session to include the invoices count.
    var invoicesCount = session.Include.Scalar<int>(invoicesQuery);

    // At this point, invoices will point to an IInclude<int> which will have it's default value of 0.

    // Load the customer.
    var customer = session.Single<Customer>(1792);

    // We can now acces the invoices count for the customer
    Console.WriteLine(invoicesCount.Value.ToString());
}

Single

The Single method has 2 overloads and allows a single instance to be included either by it’s identifier or an SqlQuery.

using (var session = sessionFactory.OpenSession())
{
    // Tell the session to include the customer.
    var includeCustomer = session.Include.Single<Customer>(3264);
        
    // At this point, includeCustomer will point to an IInclude<Customer> which will have no value.
        
    // Query to fetch the invoices for the customer.
    var invoicesQuery = new SqlQuery("SELECT * FROM Invoices WHERE CustomerId = @p0", 3264);
        
    // Load the invoices.
    var invoices = session.Fetch<Invoice>(query);
        
    // We can now acces the customer
    Console.WriteLine(includeCustomer.Value.Name);
}

This article was written for version 2.0.0 of the MicroLite framework.

Advertisements

4 thoughts on “MicroLite 2.0 Includes

  1. Pingback: MicroLite.Testing 1.0 released | MicroLite ORM

  2. Pingback: API Design Decisions | MicroLite ORM

  3. Pingback: MicroLite 4.0 – Changes to Projections and Dynamics | MicroLite ORM

  4. Pingback: MicroLite Comparison Feedback | MicroLite ORM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s