Monthly Archives: August 2012

Session Factory

As you have seen in the Configuring MicroLite post, once you have configured a connection, you create a session factory for that connection.

The session factory is used to open individual sessions to the target database and is a thread safe object which can safely exist as a singleton for the duration of your application lifecycle. It can be registered in your IOC container as a singleton or simply stored as a static property if you do not use an IOC container.

You can call the configuration process multiple times if you need to target different databases within the same application.

using MicroLite;
using MicroLite.Configuration;

static void Main(string[] args)
{
    var northwindSessionFactory = Configure
        .Fluently()
        .ForConnection("NorthwindDB")
        .CreateSessionFactory();

    var pubsSessionFactory = Configure
        .Fluently()
        .ForConnection("Pubs")
        .CreateSessionFactory();

    ...
}

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

Mapping classes to tables

MicroLite uses an opt-in approach to mapping which is achieved by applying attributes to classes and properties.

A class is mapped to a table or view by applying the TableAttribute.

using MicroLite.Mapping;

[Table(schema: "Sales", name: "Customers")]
public class Customer { }

Once the table mapping is defined, we need to add the ColumnAttribute to each property to describe which column it maps to. Any property that you wish to map must have a public get and set.

[Column("StatusId")]
public CustomerStatus Status { get; set; }

Lastly, we need to add the IdentifierAttribute to the property on the class which maps to the primary key column in the database table (note: composite keys are not supported). The IdentifierAttribute requires that we specify the IdentifierStrategy.

[Column("CustomerId")]
[Identifier(IdentifierStrategy.Identity)]
public int Id { get; set; }

There are a number of supported identifier strategies:

  • Assigned – This strategy means that the value of the identifier property is assigned before the instance is passed to ISession.Insert().
  • Guid – This strategy means that a new GUID value will be automatically assigned to the identifier property when the instance is passed to ISession.Insert() before the insert sql is generated.
  • Identity – This is the default strategy and means that the value of the identifier is generated by the database when a new row is inserted (e.g. SQL Identity or a newid() default value). This strategy will update the identifier value on the inserted object when the transaction completes.

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

Configuring MicroLite

One of the principles behind the MicroLite framework is that it should be as simple as possible to configure.

There are only 2 things you need to do to configure MicroLite, the first is to add a named connection string to the app.config (or web.config).

<connectionStrings>
    <add name="NorthwindDB"
         connectionString="Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=False;Integrated Security=SSPI;"
         providerName="System.Data.SqlClient" />
</connectionStrings>

Then, in your application start-up you need to call the configuration class to create the session factory for the database you wish to use.

using MicroLite;
using MicroLite.Configuration;

static void Main(string[] args)
{
    var sessionFactory = Configure
        .Fluently()
        .ForConnection("NorthwindDB") // The name provided here should match the name in the app.config
        .CreateSessionFactory();

    ...
}

Once you have created a session factory, you can either store it in a static property which makes it visible to your application, or ideally register it in an IOC container.

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

What is MicroLite?

A while ago, I started a small project which needed to access an Sql database. Since the project was quite small I didn’t want to go with the overhead of implementing a fully fledged ORM framework. Looking for alternatives, I came across ‘micro ORM’ frameworks. PetaPoco and Massive were the two I played about with and found that there were things I wanted to do which were difficult to achieve with either. As a result I decided to write my own and made it open source.

MicroLite was conceived with a number of principles in mind.

  • It should be simple to configure.
  • Code using it should be easily testable in isolated unit tests.
  • It should not use any external dependencies outside the .net base class libraries.
  • It should be extensible, allowing users to choose how it interacts with other frameworks (for example logging).
  • You shouldn’t have to inherit from any MicroLite classes or implement any MicroLite interfaces for your data entities.
  • It should be simple to map classes to tables without having complicated and hard to maintain XML documents.

It has been inspired by various other frameworks that I have used in the past. Having used NHibernate for a number of years, I wanted to create something that was similar so that it was easy to move between the two. Let’s be clear though, MicroLite is not a cut down implementation of NHibernate. It does not use any of the NHibernate codebase, rather it uses some similar concepts and interface names (e.g. ISession).

You can find the repository on GitHub where you can also find the Wiki and Issues log.

Over the next few weeks I will be publishing more blog posts showing some of the features and things I am working on.