Repository navigation
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/2010
}
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");
});
});
- 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 types can be configured with another call to For(…);
- It configures articles such that ArticleTitle is stored in MongoDB as “at” 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");
});
});
Finally, in some scenarios you it may find it necessary to use different configurations for different situations. NoRM’s configuration allows you to implement your own configuration map. Using standard IoC techniques, for example, you are then free to use any configuration at run time.
Here’s a sample of a custom configuration map:
///Setup your custom map
MongoConfiguration.Initialize(r => r.UseMap(new SampleConfigMap()));
/// Define you configuration in the ctor
public class SampleConfigMap : MongoConfigurationMap
{
public SampleConfigMap ()
{
For<Article>(cfg =>
{
cfg.ForProperty(a => a.ArticleTitle).UseAlias("title");
cfg.UseCollectionNamed("MyArticles");
});
For<MyOtherType>(cfg => cfg.ForProperty(p => p.Name).UseAlias("OtherTypeName"));
}
}