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

Problem with Generic Repository

Mar 26, 2013 at 9:42 PM
I think I've found a problem when using MongoRepository on a subclass. Here is some code to start with.
        var carRepo = new MongoRepository<Car>();
        var boatRepo = new MongoRepository<Boat>();

        carRepo.Add(new Car());
        boatRepo.Add(new Boat());

        Assert.AreEqual(1, carRepo.All().Count());

This test should pass because the repository for Cars should only retrieve Cars. Unfortunately it fails because the repository for Cars retrieves Boats aswell.

When I use a repository for the baseclass I can retrive the correct objects by using .OfType<T>
        var vehicleRepo = new MongoRepository<Vehicle>();

        vehicleRepo.Add(new Car());
        vehicleRepo.Add(new Boat());

        Assert.AreEqual(1, vehicleRepo.All().OfType<Car>().Count());
Should't the MongoRepository handle the OfType<T> part? At the moment it seems like it retrieves all vehicles as Cars or Boats depending on what repository you use.