[英]Serializing complex objects to mongoDB
I have a few questions that are closely related so I am grouping them under this question. 我有几个密切相关的问题,因此我将它们归为一个问题。
I'm trying to create a persistent db for my object model using c# and the mongoDB drivers and database. 我正在尝试使用c#和mongoDB驱动程序和数据库为我的对象模型创建一个持久数据库。 I want to be able to store all of my users objects - inside those users should be sessions (a "list" or similar data structure), and in each session are different events and records that they created (also lists).
我希望能够存储我所有的用户对象-这些用户内部应该是会话(“列表”或类似的数据结构),并且在每个会话中都有他们创建的不同事件和记录(也包括列表)。 When a new request is made - initially I look up the user by its identifier.
发出新请求时-最初,我通过其标识符查找用户。 If it exists I create a session object.
如果存在,则创建一个会话对象。 Add that session object to the user's session list and then make appropriate updates (adding events or records to sessions).
将该会话对象添加到用户的会话列表中,然后进行适当的更新(将事件或记录添加到会话中)。 (else I create a new user - add it to the db and then do the previous steps)
(否则,我将创建一个新用户-将其添加到数据库中,然后执行前面的步骤)
My problem is that when I do "collection.save(user)"
or "find(user)"
I am getting errors - can not serialize an abstract class. 我的问题是,当我执行
"collection.save(user)"
或"find(user)"
我遇到错误-无法序列化抽象类。 Per the documentation I should be able to use "auto-mapping" feature so I thought it would work out of the box. 根据文档,我应该能够使用“自动映射”功能,因此我认为它可以立即使用。 That would be great.
那很好啊。 I would like my db object to appear as just a container for my user objects as is in the UsersDb class.
我希望我的db对象像UsersDb类中那样只是作为用户对象的容器出现。
If not - are there appropriate "mongodb" container classes I can use (ie instead of " List<Session>
" use -> BsonList<Session>
)? 如果没有-我可以使用适当的“ mongodb”容器类(即代替“
List<Session>
”使用-> BsonList<Session>
)吗? Also how should I go about instantiating containers inside of my classes? 另外,我应该如何在类内部实例化容器? If they are generated by the serializer should I initiate them in the constructor?
如果它们是由序列化程序生成的,我应该在构造函数中启动它们吗? Also how can I store arbitrary "dynamic" data in my classes (just some regular json)
还有如何在类中存储任意的“动态”数据(只是一些常规的json)
I am creating a basic collection like this: 我正在创建一个像这样的基本集合:
public class UsersDb
{
public UsersDb()
{
MongoServerSettings settings =new MongoServerSettings();
settings.Server = new MongoServerAddress("localhost",27017);
MongoServer server = new MongoServer(settings);
MongoDatabase db = server.GetDatabase("defaultDb");
Users = db.GetCollection<User>("Users");
//Users.Drop();
}
public MongoCollection<User> Users { get; set; }
}
Here is my user class: already here i have a problem because the constructor needs to be able to create a session list - but what happens if it was serialized by the mongo driver? 这是我的用户类:在这里我已经遇到了问题,因为构造函数需要能够创建会话列表-但是,如果它由mongo驱动程序序列化会怎样?
public User()
{
SessionComparer uc = new SessionComparer();
Sessions = new List<Session>();;
}
public ObjectId Id { get; set; }
public string Udid { get; set; }
public DateTime EnrollDate { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public List<Session> Sessions { get; set; }
My session class 我的课程
public class Session
{
public Session()
{
Events = new List<Event>();
Records = new List<Record>();
}
public string SessionId { get; set; }
public DateTime Time { get; set; }
public dynamic Parameters { get; set; }
public IList<Event> Events { get; set; }
public IList<Record> Records { get; set; }
}
Records 记录
public class Record
{
public Record()
{
RecordId = Guid.NewGuid().ToString();
CreatedAt = DateTime.Now;
}
public string Name { get; set; }
public string RecordId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime Time { get; set; }
public dynamic Data { get; set; }
}
and the events 和事件
public class Record
{
public Record()
{
RecordId = Guid.NewGuid().ToString();
CreatedAt = DateTime.Now;
}
public string Name { get; set; }
public string RecordId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime Time { get; set; }
public dynamic Data { get; set; }
}
You cannot de-serialize dynamic. 您不能反序列化动态。 In order to use real classes you need to have something more strict.
为了使用真实的类,您需要有一些更严格的规定。
Probably following workaround can be suitable: 可能的以下解决方法可能是合适的:
[BsonKNownTypes(typeof(Implementation1))]
[BsonKNownTypes(typeof(Implementation2))]
public class DynamicModelHere {}
public class Implementation1 : DynamicModelHere { property, property, property }
public class Implementation2 : DynamicModelHere { property, property, property }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.