Mongodb - how to deserialze when a property has an Interface return type

Posted by Mark Kelly on Stack Overflow See other posts from Stack Overflow or by Mark Kelly
Published on 2012-10-27T01:21:00Z Indexed on 2012/10/31 23:01 UTC
Read the original article Hit count: 179

Filed under:
|

I'm attempting to avoid introducing any dependencies between my Data layer and client code that makes use of this layer, but am running into some problems when attempting to do this with Mongo (using the MongoRepository)

MongoRepository shows examples where you create Types that reflect your data structure, and inherit Entity where required. Eg.

[CollectionName("track")]
public class Track : Entity  
{
    public string name { get; set; }
    public string hash { get; set; }

    public Artist artist { get; set; }
    public List<Publish> published {get; set;}
    public List<Occurence> occurence  {get; set;}
}

In order to make use of these in my client code, I'd like to replace the Mongo-specific types with Interfaces, e.g:

[CollectionName("track")]
public class Track : Entity, ITrackEntity 
{
    public string name { get; set; }
    public string hash { get; set; }

    public IArtistEntity artist { get; set; }
    public List<IPublishEntity> published {get; set;}
    public List<IOccurenceEntity> occurence  {get; set;}
}

However, the Mongo driver doesn't know how to treat these interfaces, and I understandably get the following error:

An error occurred while deserializing the artist property of class sf.data.mongodb.entities.Track: No serializer found for type sf.data.IArtistEntity. ---> MongoDB.Bson.BsonSerializationException: No serializer found for type sf.data.IArtistEntity.

Does anyone have any suggestions about how I should approach this?

© Stack Overflow or respective owner

Related posts about interface

Related posts about mongodb-csharp