Skip to content

Getting Started

MongoDB integration in Cratis Applications is designed to be simple to set up while providing powerful defaults that work out of the box.

The simplest way to add MongoDB support to your application is through the configuration extension methods provided for WebApplicationBuilder or HostBuilder.

var builder = WebApplication.CreateBuilder(args);
builder.AddCratisArc();
builder.UseCratisMongoDB();
var app = builder.Build();
app.UseCratisArc();
var host = Host.CreateDefaultBuilder(args)
.AddCratisArc()
.UseCratisMongoDB()
.Build();

When you call UseCratisMongoDB(), the following components are automatically configured:

  • DateTimeOffset: Proper handling of timezone information
  • DateOnly: .NET 6+ date-only types
  • TimeOnly: .NET 6+ time-only types
  • System.Type: Serialization of .NET type information
  • Guid: Configured to use the standard .NET representation instead of MongoDB’s legacy format
  • Cratis Concepts: Automatic serialization for all types implementing ConceptAs<T>
  • Naming Policy Convention: Applies your configured naming policy to all properties
  • Ignore Extra Elements: Ignores unknown properties during deserialization
  • Class Maps: All implementations of IBsonClassMapFor<T> are automatically discovered and registered
  • Convention Pack Providers: All implementations of ICanProvideMongoDBConventionPacks are discovered
  • Convention Pack Filters: All implementations of ICanFilterMongoDBConventionPacksForType are discovered

You can customize the MongoDB setup by providing configuration options:

builder.UseCratisMongoDB(configureMongoDB: mongoBuilder =>
{
mongoBuilder
.WithCamelCaseNamingPolicy()
.WithServerResolver<MyCustomServerResolver>()
.WithDatabaseResolver<MyCustomDatabaseNameResolver>();
});

The framework uses resolver patterns for determining connection details:

Implement IMongoServerResolver to provide connection string logic:

public class MyServerResolver : IMongoServerResolver
{
public MongoUrl Resolve()
{
return new MongoUrl("mongodb://localhost:27017");
}
}

Implement IMongoDatabaseNameResolver to provide database naming logic:

public class MyDatabaseNameResolver : IMongoDatabaseNameResolver
{
public string Resolve()
{
return "MyApplicationDatabase";
}
}

One of the most common pain points when working with MongoDB and .NET is Guid serialization. By default, MongoDB stores Guids using a legacy binary format that can cause issues. Cratis Applications configures Guids to use the standard .NET representation, making them more predictable and interoperable.

The framework includes sensible error handling for common configuration issues:

  • Missing server resolver configuration
  • Missing database name resolver configuration
  • Missing naming policy configuration

These will throw descriptive exceptions with guidance on how to fix the configuration.