Microsoft’s ASP.Net Web API 2.2 allows you to easily create REST style APIs on an IIS website. Microsoft has some great documentation on how to get started with it, so I won’t rehash that here. Instead, I’m going to go a little deeper into some powerful features that can be used with Web API.
- Part 1 - Customizing auto-generated documentation
- Part 2 - HTTP Response Codes
- Part 3 - HTTP Error Codes from Exceptions
- Part 4 - OData URL Query Options
- Part 5 - DTO Transformations and Automapper (this article)
- Part 6 - Testing with EF Rollbacks across HTTP
First, off what is this DTO thing? A Data Transfer Object is an object structure that you use to transfer data across the wire. Sometimes it will look exactly like your entity framework entities. Sometimes it will be different.
For example, suppose you have a database with Customers and Invoices, and your tables look like this:
These are also what your Entity models look like, because entity models reflect the database structure.
Now suppose that you want to provide an API endpoint that returns a list of customers in a particular city with the number of invoices for each customer.