Using the Document Database code with RavenDB

Choosing the preset SD.RavenDB 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 RavenDB. 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 RavenDB.

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

Setting up RavenDB

To use the generated code with your RavenDB instance, you have to register a lambda to let the database determine the ID/Identity property. To do this, use the following:

documentStoreInstance.Conventions.FindIdentityProperty = 
                        p=>p.GetCustomAttribute(typeof(KeyAttribute))!=null;

Example

_store = new DocumentStore() {Url = "http://localhost:8080/", DefaultDatabase = "Northwind"};
_store.Initialize();
_store.Conventions.FindIdentityProperty = 
                        p => p.GetCustomAttributes(true).Any(a => a is KeyAttribute);

Storing fetched DTOs in RavenDB

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 RavenDB.

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 RavenDB

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

Store initialization, one time setup

private IDocumentStore _store;

//...
_store = new DocumentStore() { Url = "http://localhost:8080/", DefaultDatabase = "Northwind" };
_store.Initialize();
_store.Conventions.FindIdentityProperty = 
                                p => p.GetCustomAttributes(true).Any(a => a is KeyAttribute);

Persisting to the store

using(var session = _store.OpenSession())
{
    foreach(var c in customers)
    {
        session.Store(d);
    }
    session.SaveChanges();
}