-
Notifications
You must be signed in to change notification settings - Fork 2
3 Mongo Session Factory
Dan Geabunea edited this page Jul 22, 2015
·
1 revision
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:
-
Create a concrete implementation of the AbstarctMongoSessionFactory class. You can name it however you like.
-
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.