Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:02:27+00:00 2026-06-18T00:02:27+00:00

I am developing a project that uses MongoDB (with C# driver) and DDD .

  • 0

I am developing a project that uses MongoDB (with C# driver) and DDD.

I have a class (aggregate) which have a property which type is an interface. In another class, I have implemented this interface. This class has another property which type is an interface and is setted with another implemented class.

The code below explains better:

// Interfaces
public interface IUser {
    Guid Id { get; set;}
    IPartner Partner{ get; set; }
}

public interface IPartner {
    IPhone Mobile { get; set; }
}

public interface IPhone {
    string number { get; set; }
}

// Implemented Classes
public class User: IUser {
    [BsonId(IdGenerator = typeof(GuidGenerator))]
    public Guid Id { get; set; }

    [BsonIgnoreIfNull]
    public IPartner Partner { get; set; }
}

public struct Partner : IPartner {
    public IPhone Mobile { get; set; }
}

public struct Phone : IPhone {
    public string Number { get; set; }
}

Well, when I call the MongoCollection<User>.Insert() method, it throws two exceptions:

System.IO.FileFormatException: An error occurred while deserializing
the Partner property of class .User: An error
occurred while deserializing the Phone property of class
.Partner: Value class
.Mobile cannot be deserialized. —>
System.IO.FileFormatException: An error occurred while deserializing
the Mobile property of class .Partner: Value
class .Phone cannot be deserialized. —>
MongoDB.Bson.BsonSerializationException: Value class
.Phone cannot be deserialized.

Then, I searched the internet for discover how to deserialize the type as an interface, and I think I have to ways to do it: mapping the property with a cast, using the BsonClassMap.RegisterClassMap or writing a custom BSON serializer.

I need to know which of these two ways is better and how to implement it.

Note: I need a solution that does not modify the interfaces, because theirs project cannot contain any external reference.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:02:28+00:00Added an answer on June 18, 2026 at 12:02 am

    Well, I have found a lot of problems when trying to get this answer.

    First of all, the MongoDB C# Driver, does have some problems when deserializing interfaces, like said by Craig Wilson in this question comments, and as described in the issue page.

    The secure implementation for this problem, like I said before, really may be a custom BSON serializer or a specific class map, using BsonClassMap.RegisterClassMap.

    So, I have implemented the class map and the problem persisted.

    Looking forward with the problem, I have found that exception is related to another issue of the driver: the problem when deserializing structs.

    I have rolled back the project to the initial state (without classes map or custom serializers) and changed the struct type to class type, and it worked.

    In resume, this exception error is related to structs deserialization, not with interfaces deserialization.


    Anyway, it is a real problem, and the second issue needs to be considered more a bug than a improvement, like the first issue is.

    You can find the issues at these links:

    • https://jira.mongodb.org/browse/CSHARP-485
    • https://jira.mongodb.org/browse/CSHARP-94
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that uses an Access DB file for reference tables. This
I'm developing a ASP .NET web project that uses: jQuery, jQuery UI, reset/normalize css
I am in the middle of developing a project, which uses ASP.NET/C#. I am
I've got a project that uses Rebar as build tool. When developing, I would
I am developing a project which uses versioning: version name is typed in POM
I am developing a project that already uses XML serialization, so I need an
I take part in developing a Java project, which uses some C++ components, thus
I inherited a project that uses CoreData for a certain type of data storage.
I'm developing a project using a layered architecture. I have a DAL in which
I'm developing an Android app, that uses sereval libraries in que. That means: Project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.