反序列化对象与MongoDB的C#驱动程序接口 [英] Deserialize object as an interface with MongoDB C# Driver

查看:1341
本文介绍了反序列化对象与MongoDB的C#驱动程序接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发使用项目的 MongoDB的(用C#驱动程序)和 DDD

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; }
}



好吧,当我称之为 MongoCollection<使用者> ; .insert()这样方法,它引发两个例外:

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

System.IO.FileFormatException:发生错误反序列化
级。用户的合作伙伴属性:值类
。移动无法反序列化:反序列化类$ b $的电话特性b .Partner出错
。 --->
System.IO.FileFormatException:值
类.Phone无法反序列化:反序列化
类.Partner的移动属性发生错误。 --->
MongoDB.Bson.BsonSerializationException:值类
.Phone无法反序列化

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.

然后我在网上搜索了探索如何反序列化类型的接口,我想我有办法做到这一点:有投映射属性,使用 BsonClassMap.RegisterClassMap 或编写自定义BSON序列化。

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.

推荐答案

好吧,我发现试图得到这个答案的时候了很多问题。

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

首先,MongoDB的C#驱动程序,确实有一些问题的反序列化接口,像由Craig Wilson说,在这个问题上的意见,而当在的问题页

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.

有关这个问题的安全实施,就像我之前说的,真的可能是一个自定义的BSON串行或一个特定的类图,用 BsonClassMap.RegisterClassMap

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.

无论如何,这是一个真正的问题,需要考虑以上改善的一个bug,像第一个问题是第二个问题。

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

这篇关于反序列化对象与MongoDB的C#驱动程序接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆