Skip to content

3 Mongo Session Factory

Dan Geabunea edited this page Jul 22, 2015 · 1 revision

Mongo Session Factory

The session factory is the components that creates connections to a Mongo database. In order to set it up, there are 2 steps that you need to do:

  1. Create a concrete implementation of the AbstarctMongoSessionFactory class. You can name it however you like.

  2. Provide implementation for the AfterSessionFactoryInitialized method. Here you can register your class mappings, add indexes, etc. Basicly you globally configure the way you would like to access your database.

//create concrete implementation of mongo session factory
public class MyMongoSessionFactory:AbstractMongoSessionFactory
{
    public MyMongoSessionFactory(MongoSessionFactoryConfig configuration) : base(configuration)
    {
        //leave constructor body empty
    }

    protected override void AfterSessionFactoryInitialized(MongoDatabase mongoDatabase)
    {
        //register your defined mappings
        RegisterClassMap(new PersonMap());

        //create indexes (Name is a property of the Person class)
        GetCollection<Person>().CreateIndex(new IndexKeysBuilder().Ascending("Name"));
    }
}

Note

As stated before, you need to instantiate A SINGLE INSTANCE of the session factory class. You can achieve this using a IoC container or by creating a singleton wrapper for this class.