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

Inherited Classes and _t

Dec 7, 2012 at 9:23 AM

When I use the Mongo C# driver and specify BsonKnownTypes on my parent class my subclasses when added to the database have a "_t" key specifying the type, however I cannot get MongoRepository to do the same.

Whenever I save an object of a subclass onto the database it is saved to a collection named after the parent class but has no type indicator.

I also tried using the CollectionName attribute on the subclasses in an attempt to save them to their own collections, however MongoRepository seems to ignore this and still saves them in the parent class collection; the standard driver doesn't do this and saves them in their own collections as expected.

I am using version 1.7 Mongo C# driver and the updated MongoRepository to go with it.

I'd like to continue using MongoRepository as it's nice and intuitive to use, is there any way I can get the results I'm after?

Dec 14, 2012 at 3:45 PM
Edited Dec 14, 2012 at 3:52 PM

Could you post some code? As a simple testcase, try this:

using MongoRepository;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var repo = new MongoRepository<Vehicle>();
            repo.Add(new Vehicle[] { 
                new Car() { Name = "Audi R8", NumberOfWheels = 4 },
                new Airplane() { Name = "Boeing 747", IsJet = true },
                new Glider() {Name = "Ultralight", IsJet = false, Weight = 100 }
            });

            var vehicles = repo.All().ToList();
        }
    }

    public abstract class Vehicle : Entity  
    {
        public string Name { get; set; }
    }

    public class Car : Vehicle
    {
        public int NumberOfWheels { get; set; }
    }

    public class Airplane : Vehicle
    {
        public bool IsJet { get; set; }
    }

    public class Glider : Airplane
    {
        public double Weight { get; set; }
    }
}

 

This, for me, gives:

 

/* 0 */
{
  "_id" : ObjectId("50cb48c19a8d7c197c44199d"),
  "_t" : "Car",
  "Name" : "Audi R8",
  "NumberOfWheels" : 4
}

/* 1 */
{
  "_id" : ObjectId("50cb48c19a8d7c197c44199e"),
  "_t" : "Airplane",
  "Name" : "Boeing 747",
  "IsJet" : true
}

/* 2 */
{
  "_id" : ObjectId("50cb48c19a8d7c197c44199f"),
  "_t" : "Glider",
  "Name" : "Ultralight",
  "IsJet" : false,
  "Weight" : 100.0
}

Also, not using an abstract baseclass (vehicle) and not inheriting from Entity but implementing IEntity also works:

 

using MongoDB.Bson;
using MongoRepository;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var repo = new MongoRepository<Vehicle>();
            repo.Add(new Vehicle[] { 
                new Car() { Name = "Audi R8", NumberOfWheels = 4 },
                new Airplane() { Name = "Boeing 747", IsJet = true },
                new Glider() { Name = "Ultralight", IsJet = false, Weight = 100 },
                new Vehicle() { Name = "UFO" }
            });

            var vehicles = repo.All().ToList();
        }
    }

    public class Vehicle : IEntity
    {
        public string Name { get; set; }

        private string _id;

        public string Id
        {
            get { return _id ?? ObjectId.GenerateNewId().ToString(); }
            set { _id = value; }
        }
    }

    public class Car : Vehicle
    {
        public int NumberOfWheels { get; set; }
    }

    public class Airplane : Vehicle
    {
        public bool IsJet { get; set; }
    }

    public class Glider : Airplane
    {
        public double Weight { get; set; }
    }
}

 

This results in:

 

/* 0 */
{
  "_id" : "50cb4a879a8d7c1474c3608b",
  "_t" : "Car",
  "Name" : "Audi R8",
  "NumberOfWheels" : 4
}

/* 1 */
{
  "_id" : "50cb4a879a8d7c1474c3608d",
  "_t" : "Airplane",
  "Name" : "Boeing 747",
  "IsJet" : true
}

/* 2 */
{
  "_id" : "50cb4a879a8d7c1474c3608f",
  "_t" : "Glider",
  "Name" : "Ultralight",
  "IsJet" : false,
  "Weight" : 100.0
}

/* 3 */
{
  "_id" : "50cb4a879a8d7c1474c36091",
  "Name" : "UFO"
}