2013-05-30 68 views
2

我有幾個密切相關的問題,所以我將它們歸入這個問題。將複雜對象序列化爲mongoDB

我想創建一個持久數據庫爲我的對象模型使用c#和mongoDB驅動程序和數據庫。我希望能夠存儲我的所有用戶對象 - 在這些用戶中應該是會話(「列表」或類似的數據結構),並且在每個會話中都是他們創建的不同事件和記錄(也列出)。當發出新的請求時 - 最初我通過其標識符查找用戶。如果存在,我創建一個會話對象。將該會話對象添加到用戶的會話列表中,然後進行適當更新(向會話添加事件或記錄)。 (否則我創建一個新用戶 - 將其添加到數據庫,然後執行之前的步驟)

我的問題是,當我做"collection.save(user)""find(user)"我收到錯誤 - 無法序列化抽象類。 Per the documentation我應該可以使用「自動映射」功能,所以我認爲它可以開箱即用。那太好了。我希望我的db對象像UsersDb類中那樣只是我的用戶對象的容器。

如果不是 - 是否有適當的「mongodb」容器類可以使用(即使用「List<Session>」來代替「List<Session>」使用 - >BsonList<Session>)?另外我應該如何去實例化我的類中的容器?如果它們是由序列化器生成的,我應該在構造函數中啓動它們嗎?此外,我怎麼能存儲在我的班級任意「動態」數據(只是一些普通的json)

我創造這樣一個基本的集合:

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

這裏是我的用戶等級:已經在這裏我有一個問題,因爲構造函數需要能夠創建會話列表 - 但是如果它由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; } 

我的會話類

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

記錄

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

和事件

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

哪些類在這裏是抽象的?你能發佈完整的異常消息嗎? –

+0

這裏是我得到的消息:{「Message」:「發生錯誤」,「ExceptionMessage」:「無法創建抽象類」,「ExceptionType」:「System.MissingMethodException」,「StackTrace」:「在System.RuntimeTypeHandle.CreateInstance(RuntimeType類型,Boolean publicOnly,Boolean noCheck,Boolean&canBeCached,RuntimeMethodHandleInternal&ctor,Boolean&bNeedSecurityCheck)\ r \ n –

回答

0

不能反序列化的動態。爲了使用真實的課程,你需要有更嚴格的東西。

也許以下的解決方法可能是合適的:

[BsonKNownTypes(typeof(Implementation1))] 
[BsonKNownTypes(typeof(Implementation2))] 
public class DynamicModelHere {} 
public class Implementation1 : DynamicModelHere { property, property, property } 
public class Implementation2 : DynamicModelHere { property, property, property }