-
Notifications
You must be signed in to change notification settings - Fork 81
Configuration
NoRM supports a convention-based configuration by default and includes fluent interface for more fine-grained control of the documents persisted to the database. Take the following class as an example:
//Your model/poco public class Article { public string ArticleTitle{ get;set; } // Assume "My Article public string OriginalAuthor{ get;set; } // Assume "Walt Whitman" public DateTime DatePublished{ get;set; } // Assume 09/09/2009 }
Saving an instance of this class to MongoDB using NoRM results in the following BSON document representation.
{ "_id" : ObjectId("4b8f52b4946000000000077f"), "ArticleTitle" : "My Article", "OriginalAuthor" : "Walt Whitman", "DatePublished" : "09/09/2009" }
Naturally the size of the database will increase rapidly given the verbose property names. Ideally, the document would contain abbreviated field names in order to minimize the storage footprint.
{ "_id" : ObjectId("4b8f52b4946000000000077f"), "at" : "My Article", "oa" : "Walt Whitman", "dp" : "09/09/2009" }
Of course you’re not about to go and change your entire model to accommodate shortened field names.
The NoRM MongoConfiguration is the bridge from your model to MongoDB Documents. Let’s get right to it. Here’s a sample configuration using the Article class above.
MongoConfiguration.Initialize(config => { For<Article>(config => { config.ForProperty(u => u.ArticleTitle).UseAlias("at"); config.ForProperty(u => u.OriginalAuthor).UseAlias("oa"); config.ForProperty(u => u.DatePublished).UseAlias("dp"); }); }); /// /// OR /// MongoConfiguration.Initialize(config => config.AddMap<ArticleMap>()); // Custom type map public class ArticleMap : MongoConfigurationMap { public ArticleMap() { For<Article>(config => { config.ForProperty(u => u.ArticleTitle).UseAlias("at"); config.ForProperty(u => u.OriginalAuthor).UseAlias("oa"); config.ForProperty(u => u.DatePublished).UseAlias("dp"); }); }); } }
- It initializing a NoRM configuration container
- It setting up rules for how Article instances should be persisted.
- Unique configurations can be used for each type in your domain model.
**Additional rules for the same type can be configured with another call to For - Additional types can be configured with another call to For(…);
- Unique configurations can be used for each type in your domain model.
- It configures articles such that MongoDB stores ArticleTitle as “at,” OriginalTitle as “ot,” and so forth.
In the same way NoRM uses default conventions to project database field names, NoRM also uses your class’ type name for the MongoDB document collection name. In this example, saving an Article would produce or append to an “Article” collection. (i.e. db.Article.find()).
NoRM’s configuration also supports collection name mapping on a per-type basis. The following instructs NoRM to use the name “RecentNews” for the sample Article type:
MongoConfiguration.Initialize(config => { For<Article>(config => { config.UseCollectionNamed("RecentNews"); }); }); /// /// OR /// MongoConfiguration.Initialize(config => config.AddMap<ArticleMap>()); // Custom type map public class ArticleMap : MongoConfigurationMap { public ArticleMap() { For<Article>(config => { config.ForProperty(u => u.UseCollectionNamed("RecentNews");; }); } }