2013-04-30 43 views
3

我剛剛將我的Mongo-C#驅動程序從1.6.1更新到1.8.1,我意識到他們已經使很多功能過時了。 ,我看到,由於對棄用的錯誤之一是以下幾點:ConventionProfile是過時使用IConventionPack而不是

會議資料已經過時,請 IConventionsPack更換。

現在,問題是沒有太多有關IConeventionPack的文檔或者如何使用它。我發佈一個小的代碼片段,任何人都可以請建議如何使用IConventionPack處理這個問題?

var conventions = new ConventionProfile(); 
      conventions.SetIgnoreIfNullConvention(new AlwaysIgnoreIfNullConvention()); 
      BsonClassMap.RegisterConventions(conventions, t => true); 

謝謝。

回答

6

那麼,事實證明,沒有庫提供的IConventionPack實現。我不得不手寫一個IConventionPack的實現。下面是代碼示例:

public class OpusOneConvention : IConventionPack 
{ 
    public IEnumerable<IConvention> Conventions 
    { 
     get { return new List<IConvention> { new IgnoreIfNullConvention(true) }; } 
    } 
} 

其次:

var conventions = new OpusOneConvention(); 
ConventionRegistry.Register("IgnoreIfNull", conventions, t => true); 

所以基本上,所有的公約會作爲一個IEnumerable,然後ConventionRegistry將負責登記他們。

謝謝。

注:截至1.8.1.20版,您可以使用ConventionPack如下:

var conventions = new ConventionPack(); 
conventions.Add(new IgnoreIfNullConvention(true)); 
ConventionRegistry.Register("IgnoreIfNull", conventions, x => true); 
+2

作爲1.8.1.20存在一個庫提供的實現在這裏:http://api.mongodb.org/ CSHARP /電流/ HTML/28ac3635-dfe6-41bf-d29f-8fcd9c27715a.htm – 2013-06-18 03:41:37

相關問題