Help Documentation
Help documentation is currently included with each
release and with the
source code.
Before Save Processes
Before save processes were designed to allow for any processing that must happen to the model before each save. This can include additional validation or setting of auditing values.
ReadWriteService<T>.AddBeforeSaveProcess(Func<T, bool> process) -> returns void;
In the example below, the
Save() method of the service will call any anonymous methods that were registered via the
ReadWriteService<T>.AddBeforeSaveProcess(Func<T, bool> process) method. The anonymous method must return a
bool . If
true is returned from the anonymous method, the save will proceed as normal. If
false is returned from the anonymous method, the save will not occur and the
Save() method will return
SaveResult.BeforeSaveFailure
BlogPostService.AddBeforeSaveProcess(bp => bp.UrlSlug != "");
var post = new BlogPost { ... };
var saveResult = BlogPostService.Save();
After Save Processes
After save processes were designed to all for any processing that must happen after the model is saved to the mongoDB database. These types of items may include storing audit or summarization data.
ReadWriteService<T>.AddAfterSaveProcess(Action<T> process) -> returns void;
Configuration & Initialization
MongoServices configuration is done via static methods in the
MongoServicesModel ,
ReadWriteRepository<T> , and
ReadWriteService<T> classes. There are two steps that are required to execute the initialization process.
- Create a class that implements one or more of the following interfaces: IMongoServicesModelInitializer , IMongoServicesRepositoryInitializer and IMongoServicesServiceInitializer .
- Call the MongoServicesConfig.Initialize() method from some where in your application.
- Note: This method must only be called once otherwise any of your configurations will be duplicated within the MongoServices framework.
For example, in an ASP.Net MVC application, you would most likely want to call this method in the
Application_Start() method in your
Global.asax.cs file. Here is the sample code from example web application contained in the source code.
public class ServiceConfig : IMongoServicesServiceInitializer
{
public void Setup()
{
BlogPostService.AddBeforeSaveProcess(BlogPostService.AfterSaveProcess);
}
}
Connection String
By default, MongoServices will look for a connection string named "MongoServiceConnectionString" in your app.config or web.config file. Here is the connection string from the web.config of the example application.
<connectionStrings>
<add name="MongoServiceConnectionString" connectionString="mongodb://localhost/MongoServicesExample"/>
</connectionStrings>
However, you can override this behavior by creating a static method that returns a
MongoUrl . This can be done at a global level or at a repository level. Below are the methods that you need to call to set those values. This provides you with the flexibility to store models in different database, store all models in the same database or any other logic that you want to provide.
Globally
MongoServicesConfig.SetMongoUrlProvider(Func<MongoUrl> provider) -> returns void;
Repository
MongoServicesRepository<T>.SetMongoUrlProvider(Func<MongoUrl> provider) -> returns void;