Using the Document Database code with a generic document database.
Choosing the preset SD.GenericDocDB
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 a generic document database like Microsoft DocumentDB and RethinkDB. 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 the generic document database.
'Generic Document Database' is basically a document database that uses JSon serialization and uses Newtonsoft's JSon.NET to specify ID fields/properties, and additionally doesn't require any other setup code. The generated code is usable on RethinkDB and Microsoft DocumentDB but also other document databases or NoSQL stores, as long as they use the same requirements.
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 RethinkDB instance. RethinkDB doesn't require any setup code to use the generated codebase. The examples below use the open source RethinkDB-net client.
Storing fetched DTOs in RethinkDB
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 RethinkDB.
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 RethinkDB
After the DTOs are fetched from the relational database, they can be persisted to the RethinkDB instance.
Client initialization
private IConnectionFactory _connFactory
//...
// connect to the IP address of your RethinkDB server.
_connFactory = RethinkDb.Newtonsoft
.Configuration.ConfigurationAssembler
.CreateConnectionFactory("myCluster");
This uses the configuration as defined below, which is placed in the application's app/web.config file.
<rethinkdb>
<clusters>
<cluster name="myCluster">
<!-- <defaultLogger enabled="true" category="Debug"/> -->
<defaultLogger enabled="true" category="Warning" />
<connectionPool enabled="true" queryTimeout="300" />
<networkErrorHandling enabled="true" />
<endpoints>
<endpoint address="192.168.0.54" port="28015" />
</endpoints>
</cluster>
</clusters>
</rethinkdb>
Persisting to the database
using(var conn = _connFactory.Get())
{
var db = Query.Db("Northwind");
var table = db.Table<Customer>("Customer");
conn.Run(table.Insert(customers));
}