This project has moved and is read-only. For the latest updates, please go here.

Change db name dynamically

Jan 21, 2015 at 8:50 PM
Edited Jan 21, 2015 at 8:54 PM
Hello.
How i can change db name for MongoRepository dynamically at run time?

In you documentation you write:

"If you don't want that, or want to provide more than one connectionstring then take a look in the Advanced usage section below."

but there is nothing about that.
Jan 27, 2015 at 11:04 AM
You can pass a collectionname to the MongoRepository constructor:
using MongoRepository;
using System.Configuration;

class Program
{
    static void Main(string[] args)
    {
        var cnnstr = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;

        var x = new MongoRepository<Foo>(cnnstr, "XCollection");
        x.Add(new Foo { Name = "X test" });

        var y = new MongoRepository<Foo>(cnnstr, "YCollection");
        y.Add(new Foo { Name = "Y test" });
    }
}

class Foo : Entity
{
    public string Name { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="MyDB" connectionString="mongodb://localhost/MyDatabase" />
  </connectionStrings>
</configuration>
Depending on your needs however, you could also use derived types to use different collections like this:
using MongoRepository;
using System.Configuration;

class Program
{
    static void Main(string[] args)
    {
        var x = new MongoRepository<XFoo>();
        x.Add(new XFoo { Name = "X test" });

        var y = new MongoRepository<YFoo>();
        y.Add(new YFoo { Name = "Y Test" });
    }
}

class Foo : Entity
{
    public string Name { get; set; }
}

[CollectionName("FooX")]
class XFoo : Foo
{
}


[CollectionName("FooY")]
class YFoo : Foo
{
}
You could also derive from MongoRepository<T> so you don't need to use a connectionstring every time you instantiate a repository:
using MongoRepository;
using System.Configuration;

class Program
{
    static void Main(string[] args)
    {
        var x = new FooRepo("XCol");
        x.Add(new Foo { Name = "X test" });

        var y = new FooRepo("YCol");
        y.Add(new Foo { Name = "Y Test" });
    }
}

class FooRepo : MongoRepository<Foo>
{
    public FooRepo(string colName)
        : base(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString, colName)
    {
    }
}

class Foo : Entity
{
    public string Name { get; set; }
}
Or, a bit more generic:
using MongoRepository;
using System.Configuration;

class Program
{
    static void Main(string[] args)
    {
        var x = new MyRepo<Foo>("XCol");
        x.Add(new Foo { Name = "X test" });

        var y = new MyRepo<Foo>("YCol");
        y.Add(new Foo { Name = "Y Test" });

        var z = new MyRepo<Bar>("BarCol");
        z.Add(new Bar { FooBar = "BarTest" });
    }
}

class MyRepo<T> : MongoRepository<T>
    where T: Entity
{
    public MyRepo(string colName)
        : base(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString, colName)
    {
    }
}

class Foo : Entity
{
    public string Name { get; set; }
}

class Bar : Entity
{
    public string FooBar { get; set; }
}
etc. etc.

There are plenty of options ;-)
Feb 9, 2015 at 6:49 PM
Hi,

I am creating Web Api project. I was able to connect to different databases based on the above answer.

Also, I am trying to use unity to inject mongoRepository in my other repositories. The primary need for this was to test mongoRepository. I am using MockMongoRepository based on the discussion from this link.

Individually I am able to do both the things. i.e. 1. Connecting to different dbs dynamically and 2. Creating mock and testing mongoRepo using unity.
I am stuck at a problem where I am trying to do both at once.

Problem:
I am trying to inject mongo repository in Unity ->
 container.RegisterType<IRepository<T>, MyRepo<T>>(new InjectionConstructor());  OR
 container.RegisterType<IRepository<T>, MongoRepository<T>>(new InjectionConstructor());
and trying to call
_injectedRepoInstance.Add(document);
But the _injectedRepoInstance always saves to the default database. I want it to access different databases based on the headers in my web request. I have created a helper class which initializes the static connection string variable. But that process is in a custom attribute which is executed after unity is done creating objects.

Is there any way to set the connection string or pass the connection string after creating the mongoRepo object?