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.

Advertisements

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