Using the Document Database code with MongoDB

Choosing the preset SD.MongoDB will produce for the derived model using the 'Document Database' as target framework a DTO class model and IQueryable<T> based projection code to materialize the DTOs from a relational database to persist them into MongoDB. See Document Database presets for details.

The IQueryable<T> projection methods are optional. If you decide not to generate code for the Entity Model in your LLBLGen Pro project, no IQueryable<T> methods are available. Using the projection methods is equal to fetching DTOs in the Data Transfer Object class model. The idea behind the projection methods in this context is to store denormalized, read-only data in a document database for fetch optimization in your application. If you don't use a relational database and want to use the document database as the only database in your application, you can opt to not generate the Entity Model code, and simply use the generated DTO class model for read/write on MongoDB.

Info

If you don't generate an Entity Model, even though no IQueryable<T> methods are generated, there's still a RootNamespace.Persistence Visual Studio project generated, which in this case only contains the BsonClassMappings mapping class.

The example below fetch a set of Customer DTO instances from the relational database through various ORMs and persists the DTO instances in a MongoDB instance.

Setting up MongoDB

To use the generated code with your MongoDB instance, you have to execute one line of code at the start of the program:

BsonClassMappings.RegisterClassMappings();

BsonClassMappings requires a reference to MongoDB.Driver, the official MongoDB driver, available on nuget.

Storing fetched DTOs in MongoDB

The example first shows how to fetch the DTOs from the relational database, for each supported ORM. After that the fetched DTOs are stored in MongoDB.

Fetching the DTOs from the relational database

The following code is used to fetch all data of the customers from 'USA' into DTOs, which are then stored in a List<Customer> called customers.

List<Customer> customers = null;
using(var adapter = new DataAccessAdapter())
{
    var metaData = new LinqMetaData(adapter);
    customers = metaData.Customer
                            .Where(c=>c.VisitingAddressCountry == "USA")
                            .ProjectToCustomer()
                            .ToList();
}
List<Customer> customers = null;
using(var ctx = new NorthwindDataContext())
{
    customers = ctx.Customers
                    .Where(c=>c.VisitingAddress.Country == "USA")
                    .ProjectToCustomer()
                    .ToList();
}
List<Customer> customers = null;
using(var ctx = new NorthwindDataContext())
{
    customers = ctx.Customers
                    .Where(c=>c.Country == "USA")
                    .ProjectToCustomer()
                    .ToList();
}
List<Customer> customers = null;
using(var session = SessionManager.OpenSession())
{
    customers = session.Query<Customer>()
                    .Where(c=>c.VisitingAddress.Country == "USA")
                    .ProjectToCustomer()
                    .ToList();
}

Writing the fetched DTOs to MongoDB

After the DTOs are fetched from the relational database, they can be persisted to the MongoDB instance.

Mapping initialization, one time setup

// somewhere at the start of your application
BsonClassMappings.RegisterClassMappings();

Client initialization

private MongoClient _client;

//...

// connect to the IP address of your MongoDB server.
_client = new MongoClient("mongodb://192.168.0.54:27017");

Persisting to the database

The MongoDB client is fully async, so the code below is async only.

var nwDatabase = _client.GetDatabase("Northwind");
var collection = nwDatabase.GetCollection<Customer>("Default");
await collection.InsertManyAsync(customers);