MicroLite 3.0 Enhancements

MicroLite 3.0.0 adds support for PostgreSQL and MySQL, provides some enhancements and contains a few bug fixes.

Changes

Identifier Strategies

IdentifierStrategy.AutoIncrement and IdentifierStrategy.Identity have been replaced with a generic IdentifierStrategy.DbGenerated. There are a couple of reasons behind this:

  • It makes it easier to use SQLite for integration tests if you use MS SQL.
  • The AutoIncrementListener and IdentityListener were identical and could be consolidated into a single Listener (DbGeneratedListener).

Transactions

Transactions will now automatically rollback on dispose if they haven’t already been rolled back and failed to commit (either commit threw an exception or was not called). Essentially all this means is that you don’t have to write as much boilerplate code.

// If you are just calling rollback and throwing the exception back up, this common concept:
using (var transaction = session.BeginTransaction())
{
    try
    {
        //... do some stuff with session
        transaction.Commit();
    }
    catch
    {
        transaction.Rollback();
        throw;
    }
}

// Can be replaced with this:
using (var transaction = session.BeginTransaction())
{
    //... do some stuff with session
    transaction.Commit();
} // if the transaction fails, it will be automatically rolled back here.

Read Only Session

The ISession interface has been split in 2, the ISession interface still exists but the query functionality has been extracted into a separate interface IReadOnlySession. The interfaces look like this:

MicroLite 3 Session Interfaces

As you can see, ISession extends IReadOnlySession so your existing code will continue to function as before. BeginTransaction is on IReadOnlySession so that you can enforce an isolation level when performing multiple reads.

The ISessionFactory now contains an additional method which will return an IReadOnlySession

MicroLite 3 Session Factory

ISession also now contains a new method InsertOrUpdate which given an object, will decide whether it should be inserted or updated based upon it’s identifier value. This can save you having to write code like this in a web service/api.

// Instead of:
if (entity.Id == 0)
{
    session.Insert(entity);
}
else
{
    session.Update(entity);
}

// You can now call:
session.InsertOrUpdate(entity);

Obviously InsertOrUpdate can cater for any supported identifier type (int, Guid etc).

Paging Options

The ISession.Paged method has a new overload which accepts a PagingOptions. This allows us to set the paging values with more control.

There are 2 static methods to create a PagingOptions:

ForPage – this replicates the old paging behaviour, specifying the page number and a number of results per page.

var customers = session.Paged<Customer>(query, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

SkipTake – this allows for more flexible paging where you don’t necessarily want to have to skip a number of results based upon pages (e.g. in an API/Service where the skip and take are supplied by the caller).

var customers = session.Paged<Customer>(query, PagingOptions.SkipTake(skip: 12, take: 8));

Include.HasValue

The IInclude and IIncludeMany interfaces have been updated to include a HasValue property. This property will be false until the queries have been executed at which point it will be set to true only if a result is returned.

This adds a consistent way to determine whether the include has a value as previously when checking the Value property for a query returning a value type (e.g. an integer) it was impossible to know the difference between 0 being returned and no results for the query.

Obsolete

  • IdentifierStrategy.AutoIncrement – replaced by IdentifierStrategy.DbGenerated.
  • IdentifierStrategy.Identity – replaced by IdentifierStrategy.DbGenerated.
  • IReadOnlySession.Paged(SqlQuery sqlQuery, int page, int resultsPerPage) – replaced by IReadOnlySession.Paged(SqlQuery sqlQuery, PagingOptions pagingOptions).

MicroLite.Listeners.AutoIncrementListener and MicroLite.Listeners.IdentityListener have been deleted and replaced by MicroLite.Listeners.DbGeneratedListener, if you are using the Listener Performance Tweak, you will need to update your code accordingly.

Bug Fixes

New Features

  • Support for MySQL – details will be posted separately
  • Support for PostgreSQL – details will be posted separately
  • A convention based mapping option – details will be posted separately
Advertisements

2 thoughts on “MicroLite 3.0 Enhancements

  1. Pingback: Convention Based Mapping | MicroLite ORM

  2. Pingback: What’s coming after 3.0? | 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