Marten is a .NET document database library that uses an underlying PostgreSQL database to store objects as JSON.
Ordinarily, Marten takes care of retrieving the JSON from the database and deserializing it into an object. We can however instruct Marten to perform a query to retrieve document(s) and not perform the deserialization, instead giving us the JSON as it appears in the underlying PostgreSQL record. If we are exposing documents via a Web API, this feature can be taken advantage of to reduce some processing overhead on the server.
As an example, we could have the following Customer document defined:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
// etc.
}
And in a CustomersController we could start with a method as follows to add Customers to the database:
public void Post(Customer customer)
{
// no validation
// DocumentStore would normally be created only once in app, e.g. via IOC singleton
using (var store = DocumentStore.For(ConnectionString))
{
using (var session = store.LightweightSession())
{
session.Store(customer);
session.SaveChanges();
}
}
}
To get all Customers, the following method could be written:
// GET api/customers
public IEnumerable<Customer> Get()
{
// DocumentStore would normally be created only once in app, e.g. via IOC singleton
using (var store = DocumentStore.For(ConnectionString))
{
using (var session = store.QuerySession())
{
return session.Query<Customer>();
}
}
}
The preceding method however incurs the additional overhead of Marten deserializing the database JSON into Customer objects, only to be serializing it again back into JSON on the way out of the API.
When creating the LINQ query, the Marten ToJsonArray() method can be added to instruct Marten to simply return the JSON directly from the database.
We can then modify the Get method as follows:
public HttpResponseMessage Get()
{
// DocumentStore would normally be created only once in app, e.g. via IOC singleton
using (var store = DocumentStore.For(ConnectionString))
{
using (var session = store.QuerySession())
{
string rawJsonFromDb = session.Query<Customer>().ToJsonArray();
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(rawJsonFromDb, Encoding.UTF8, "application/json");
return response;
}
}
}
We could also write the parameterized Get method and use Marten’s AsJson() method to get the JSON string for the individual Customer document as in the following code:
public HttpResponseMessage Get(int id)
{
// DocumentStore would normally be created only once in app, e.g. via IOC singleton
using (var store = DocumentStore.For(ConnectionString))
{
using (var session = store.LightweightSession())
{
var rawJsonFromDb = session.Query<Customer>().Where(x => x.Id == id).AsJson().FirstOrDefault();
if (string.IsNullOrEmpty(rawJsonFromDb))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(rawJsonFromDb, Encoding.UTF8, "application/json");
return response;
}
}
}
To learn more about the document database features of Marten check out my Pluralsight courses: Getting Started with .NET Document Databases Using Marten and Working with Data and Schemas in Marten.
You can start watching with a Pluralsight free trial.
SHARE: