Skip to content

2 Mongo Session Factory Configuration

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

Mongo Session Factory Configuration

This component is a simple class that lets you set up the session factory configuration. It provides properties for server address, port, username, password, authentication method, mongo URI, etc. The method provides 2 different constructors:

Create from mongo URI

//create mongo config from uri
var mongoConfig = new MongoSessionFactoryConfig("mongodb://localhost:27016/personsdb?safe=true")
{
    //define the collections
    Collections = new Dictionary<Type, string>()
    {
        //objects of type Person will be saved in the Persons collections
        {typeof (Person), "Persons"}
    }
};

Build configuration manually

//create mongo config manually for more control
var mongoConfig = new MongoSessionFactoryConfig
{
    Server = "localhost",
    Port = 27016,
    Database = personsdb,
    User = "user",
    Password = "password",
    CredentialType = MongoCredentialType.MONGODB_CR,
    Collections = new Dictionary<Type, string>
    {
        {typeof(Person), "Persons"},
        {typeof(Vehicle), "Vehicles"}
    }
};

NOTE

Notice the Collections property. It is a dictionary that maps an object type to a collection name. We can define, for example, that objects of type 'Person' will be saved in a collection named 'Persons' in our database. This is how MongoBasic allows you to eliminate specifying the collection name when performing operations.