This project has moved. For the latest updates, please go here.

Configuring IoC with MongoRepository

May 5, 2012 at 6:51 AM
Edited May 5, 2012 at 6:55 AM

Please post some examples on how we should configure IoC containers using MongoRepository.

Actually i'm trying with Autofac Container, registering like this:

builder.RegisterType<MongoRepository<IEntity>>().As<IRepository<IEntity>>().InstancePerLifetimeScope();
//or like this
builder.Register(x => new MongoRepository<IEntity>()).As<IRepository<IEntity>>().InstancePerLifetimeScope();

but i got an exception that states the Database Parameter is null in the Default Constructor that takes the connection string from the Web.Config, without specifying a database, and when specifying the database i get:

Object reference not set to an instance of an object.

Using this connection string:  "mongodb://localhost:27017/local?safe=true"

May 25, 2012 at 4:43 AM
Edited May 25, 2012 at 1:47 PM

I have the same problem, I figured out that MongoRepository doesn't work with interface type as generic parameters of repository. It must be a concrete class.

 

For example:

 

 

IRepository<IMyInterface> repo = new MongoRepository<IMyInterface>(); //doesn't works.

 

 

Should be:

 

IRepository<MyConcreteClass> repo = new MongoRepository<MyConcreteClass>();

 

So, your container must be configured to inject the resolved generic type.