IT:AD:ASP.NET:WebAPI:HowTo:Use OData and DTOs
Summary
Process
A key goal would be to use a Repository, returning DTO's to external services. That's a bit tougher.
- The DataModel is defined in the Application Layer.
- Use a DataMapper such as what we developed in XActLib (XAct.ObjectMapping.IMappingService that wrapped IT:AD:AutoMapper):
- DataMapper can Include serialize child items.
Example:
The trick is that the Constructor is creating queryable property.. That can later be exposed over the wire. Neat.
public class DataModel
{
public DataModel()
{
using (var dbContext = new ODataDemoEntities())
{
Invoices =
dbContext.Invoices
//.Include("LineItems")
//.Where(i=>i...)
//.Skip(...)
//.Take(...)
.Select(i=> {return _DataMappingService.Map<InvoiceDTO>(i))
}
}
public IQueryable<EmployeeDTO> Invoices { get; private set; }
}
Resources
* Found it (later, mind you!) here: http://stackoverflow.com/a/9806623