MicroLite 6.0 – Specifying the DbType

In MicroLite 6.0, we have exposed the ability to control the mapping between a .NET Type and the Database Type (DbType).

If not explicitly configured, the types will be mapped using the default mappings – you can see the default mappings here

If you use the Attribute based mapping and want to specify the DbType for a specific property, you can set it in the ColumnAttribute applied to that property:

[Column("DoB", DbType.DateTime2)]
public DateTime DateOfBirth { get; set; }

If you use convention based mapping and want to specify the DbType for a specific property, you can define that in your own convention:

        new ConventionMappingSettings
        {
            ResolveDbType = (PropertyInfo propertyInfo) =>
            {
                if (propertyInfo.PropertyType == typeof(DateTime) && propertyInfo.ReflectedType == typeof(Customer))
                {
                     return DbType.DateTime2;
                }

                // Use the default
                return TypeConverter.ResolveDbType(propertyInfo.PropertyType);
            }

If you always want to use a specific DbType for a .NET Type, you can set it via the TypeConverter class:

// Always use DbType.DateTime2 instead of DbType.DateTime for System.DateTime
TypeConverter.RegisterTypeMapping(typeof(DateTime), DbType.DateTime2);

MicroLite 6.0 – Dynamic Connection Strings

Prior to MicroLite 6.0, in order to create a session factory you had to specify the connection name as defined in the connection strings section of your application configuration. This is generally a good idea as the connection string is a configuration value which should not be baked into the code as you would need a different value if you have separate Dev/Test/Production environments.

We did have a request from a MicroLite user to be able to specify the connection string at runtime as there is a requirement for that in his application so we have added functionality for doing just that.

The ForConnection methods now have an additional overload:

ForMsSql2005Connection(string connectionName, string connectionString, string providerName)

The reason that the new overload needs a connection name is to still allow support for the MVC and WebApi extensions which are dependent on a named connection to resolve the correct session factory.

The provider name needs to be the same as you would set in the connection string in the application config, this is used by MicroLite to resolve the correct DbProviderFactory.

This gives you two ways to configure a connection in MicroLite 6.0:

Option 1 (the recommended approach for most use cases)

Define the connection string in the application configuration

<connectionStrings>
    <add name="NorthwindDB"
         connectionString="Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=False;Integrated Security=SSPI;"
         providerName="System.Data.SqlClient" />
</connectionStrings>
// Create the session factory for the named connection
var sessionFactory = Configure
        .Fluently()
        .ForMsSql2005Connection("NorthwindDB")
        .CreateSessionFactory();

Option 2 (if you really really need to)

// Create the session factory with the specified name, connection string and provider name
var sessionFactory = Configure
        .Fluently()
        .ForMsSql2005Connection(
               "NorthwindDB",
               "Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=False;Integrated Security=SSPI;",
               "System.Data.SqlClient")
        .CreateSessionFactory();

Upgrading to MicroLite 6.0

There are a few changes in MicroLite 6.0 and some of the extensions which you need to be aware of when updating from a previous version.

The .NET 4.5 build now contains an Async version of the session api (IAsyncSession and IAsyncReadOnlySession). If you are using a .NET 4.5 app, I suggest you update to make full use of the async version. The MVC and WebApi extensions already exclusively make use of it (see further below for more details).

Here’s the .NET 4.5 async version of each session method:

.NET 4.0 API .NET 4.5 API
Read API
Session.Fetch(SqlQuery) await Session.FetchAsync(SqlQuery)
Session.Paged(SqlQuery) await Session.PagedAsync(SqlQuery)
Session.Single(obj) await Session.SingleAsync(obj)
Session.Single(SqlQuery) await Session.SingleAsync(SqlQuery)
Write API
Session.Delete(obj) await Session.DeleteAsync(obj)
Session.Insert(obj) await Session.InsertAsync(obj)
Session.Update(obj) await Session.UpdateAsync(obj)
Session.Advanced.Delete(Type, obj) await Session.Advanced.DeleteAsync(Type, obj)
Session.Advanced.Execute(SqlQuery) await Session.Advanced.ExecuteAsync(SqlQuery)
Session.Advanced.ExecuteScalar(SqlQuery) await Session.Advanced.ExecuteScalarAsync(SqlQuery)
Session.Advanced.Update(ObjectDelta) await Session.Advanced.UpdateAsync(ObjectDelta)

Attribute Mapping

If you use the attribute based mapping, the attributes are now in the MicroLite.Mapping.Attributes namespace rather than MicroLite.Mapping.

DbEncryptedString

The DbEncryptedString class and associated classes has moved to the new MicroLite.Extensions.Cryptography project

SQL Builder

The Delete and Update Builder WhereEquals method has been replaced with Where().IsEqualTo() to align it with the Select Builder

MicroLite.Extensions.Mvc

Method signature needs to change from:

public ActionResult MethodName(args)

to

public async Task<ActionResult> MethodName(args)

MicroLite.Extensions.WebApi

Method signature needs to change from:

public HttpResponseMessage Get(int id)

to

public Task<HttpResponseMessage> Get(int id)

example:

public Task<HttpResponseMessage> Get(int id)
{
    return this.GetEntityResponseAsync(id);
}

MicroLite Extension Release Candidates

A release candidate for all the MicroLite extensions has been released built against MicroLite 6.0-rc. As with the MicroLite 6.0-rc, these are still to be considered pre-release so don’t roll them into production just yet!

When the final build of each extension is released along side MicroLite 6.0, I will publish full release notes and an upgrade guide but here’s a couple of pointers if you want to try out the release candidates:

Mvc and WebApi extension

The .NET 4.5 build of both the extensions now use the Async version of the Session API added in MicroLite 6.0, if you use the .NET 4.5 build you will need to use IAsyncSession instead of ISession and IAsyncReadOnlySession instead of IReadOnlySession.

WebApi extension

The method signature for the MicroLiteApiController<T> and the MicroLiteODataApiController<T> helpers has changed from:

protected virtual HttpResponseMessage ...

to

protected virtual Task<HttpResponseMessage> ...

So for the Get method you would need to update from:

public HttpResponseMessage Get(int id)
{
    return this.GetEntityResponse(id);
}

to

public Task<HttpResponseMessage> Get(int id)
{
    return this.GetEntityResponse(id);
}

DbEncryptedString

The DbEncryptedString class has now moved to a new package MicroLite.Extensions.Cryptography.

MicroLite 6.0 RC

The release candidate for MicroLite 6.0.0 has been released on NuGet, please feel free to download it and try it out.

This contains fixes for 3 bugs I found in beta 2 whilst running some integration tests with each of the supported database servers.

The RC build will be re-released as the final 6.0.0 build unless any further defects are uncovered, I expect to publish the final release some time before the end of November 2014.

If you find any bugs, please raise them in the GitHub Issues along with any necessary information to reproduce it (or better yet, a pull request with a failing unit test) and we’ll make sure it’s squashed before the final release.

MicroLite 6.0 Beta 2

A second beta for MicroLite 6.0.0 has been released on NuGet, please feel free to download it and try it out.

This contains a fix for a bug I found in the previous beta whilst updating some of the extensions to work with the new async methods.

There are some breaking changes in MicroLite 6.0 which make them incompatible with some functionality in the current versions of the Mvc extension (5.3.0) and WebApi extension (4.3.0). The extensions will be updated alongside MicroLite 6.0 when the final build is released but at this point in time, there are no beta builds for the extensions.

Remember, this is a beta so use it for testing only, not production code!

If you find any bugs, please raise them in the GitHub Issues along with any necessary information to reproduce it (or better yet, a pull request with a failing unit test) and we’ll make sure it’s squashed before the final release.

MicroLite 6.0 Beta

A beta for MicroLite 6.0.0 has been released on NuGet, please feel free to download it and try it out.

We will cover the changes in a series of blog posts, but here are the main things to be aware of:

  • Support for async/await in the session API in the .NET 4.5 build
  • Attribute mapping has moved namespace to MicroLite.Mapping.Attributes
  • DbEncryptedString has been moved into its own MicroLite.Extensions.Cryptography project and package – this will be published along side the final MicroLite 6.0 build
  • Support for specifying the DbType of a property
  • Support for dynamic connection strings (in addition to named connection strings)
  • Updates to the Delete and Update SqlBuilders to make them more on par with the Select builder
  • Improved logging and debugging support
  • Removed methods marked as obsolete in MicroLite 5.0-5.3

Unfortunately, we have not had time to finish the Oracle and Sybase implementations so they won’t be included in 6.0, they will have to wait for MicroLite 6.1.

Remember, this is a beta so use it for testing only, not production code!

If you find any bugs, please raise them in the GitHub Issues along with any necessary information to reproduce it (or better yet, a pull request with a failing unit test) and we’ll make sure it’s squashed before the final release.