govin
govin

Reputation: 6693

Deserializing abstract types in MongoDB and JSON.NET

I have a class like below.

public abstract class Employee
{
   public abstract string EmployeeType {get;}
}

public class Engineer: Employee
{
   public override string EmployeeType 
   { 
      get
      {
         return "engineer";
      } 
   }    
}

I have another class which has a List of Employees, that can have different types of employees (Engineer, Manager etc.). This class serializes well into my Mongo database. However, for deserializing, how do I specify the deseralizer to deserialize the Employee record to either Engineer or Manager objects based on the EmployeeType string. How do I specify this to the MongoDB deserializer (for database layer) and JSON.NET deserializer (for middle tier)?

Upvotes: 1

Views: 789

Answers (1)

carlos.boeing
carlos.boeing

Reputation: 19

The best way to do this is using mongo type discriminators. Here's a good article about it:

http://mycodeonline.com/peterkneale/blog/mongo-type-discriminators

Hope it helps!

Upvotes: 1

Related Questions