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

Exception in MongoRepository ctor

Dec 30, 2013 at 4:55 PM
I created a custom repository that inherits from the MongoRepository. I want to use custom connection string names so my custom repository has a couple of constructors that pass its respective parameter to the base class constructor - one takes a string and one takes a MongoUrl.

The custom repository is defined like this
public class MyTypeRepository 
         : MongoRepository<MongoMyType, Guid>, IMyTypeRepository
The constructors are defined like this
    public MyTypeRepository(MongoUrl mongoUrl) : base(mongoUrl) { }

    public MyTypeRepository(string connectionString) 
            : base(connectionString) { }
When I try to construct a MyTypeRepository using either the MongoUrl or a string connectionstring, I experience the following Exception:
SetUp : System.NullReferenceException : Object reference not set to an instance of an object.
   at MongoRepository.Util`1.GetCollectionNameFromType(Type entitytype)
   at MongoRepository.Util`1.GetCollectionName()
   at MongoRepository.Util`1.GetCollectionFromConnectionString(String connectionString)
   at MongoRepository.MongoRepository`2..ctor(String connectionString)
   at Infrastructure.Data.MongoDb.MyTypeRepository..ctor(String connectionString) in    MyTypeRepository.cs: line 18
   at Test.Integration.Infrastructure.Data.MongoDb.MyTypeRepositoryTester.SetUp() in MyTypeRepositoryTester.cs: line 32
I don't even really know where to begin. If I am missing something or doing something that is obviously wrong, I would really appreciate a steer.

Thanks,
-wp
Coordinator
Jan 2, 2014 at 2:02 PM
I tried to reproduce your problem but for me it seems to work perfectly fine? I had to "fill in the blanks" for MongoMyType and IMyTypeRepository so maybe there's a difference in that respect? This is my attempt to reproduce your issue in an Console application:
using MongoDB.Driver;
using MongoRepository;
using System;

class Program
{
    static void Main(string[] args)
    {
        var x = new MyTypeRepository("mongodb://localhost/MyDatabase");
        var y = new MyTypeRepository(new MongoUrl("mongodb://localhost/MyDatabase"));
    }
}

public class MyTypeRepository
         : MongoRepository<MongoMyType, Guid>, IMyTypeRepository
{
    public MyTypeRepository(MongoUrl mongoUrl) : base(mongoUrl) { }

    public MyTypeRepository(string connectionString)
        : base(connectionString) { }
}

public interface IMyTypeRepository
{
    //???
}

public class MongoMyType : IEntity<Guid>
{
    public Guid Id { get; set; }
    //???
}

Could you point out what I need to change in the above code to reproduce your problem?
Jan 6, 2014 at 9:04 PM
Thanks for your response. :-)

My solution is very diligently following DDD and the Onion Architecture. So we are not allowing any infrastructure in our domain model. Since Mongo requires that the ID be decorated with an attribute, we are basically creating a Mongo version of the Aggregated Root; which equates to a Mongo Document. So the definition of MongoMyType is actually empty. There is no Id or anything else defined in this type. The type actually inherits from MyType.
public class MongoMyType : MyType, IEntity<Guid> {}

The problem was caused during the instantiation of MyTypeRepository. There was a null reference on GetCollectionName. The solution to the problem was pretty simple once we came to a better understanding of how MongoRepository works. We had to decorate MongoMytype with a collection name
[CollectionName("MyType")]
public class MongoMyType : MyType, IEntity<Guid>{}

Works like a charm.

Thanks again!!