Now, let’s make sure our basic app is up and running.
We will be using Postman to test our controller.
Fire it up, start your Web Api project, and execute a ‘GET’ request against your new endpoint ‘/api/example/status’

Now we can start to introduce some Popcorn!

To get started, create a ‘Models’ subfolder in your project, and add two classes: Employee and Car.

Now we need to introduce this data into the application as a dependency injection. Modify Startup::ConfigureServices:

// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){vardatabase=CreateExampleDatabase();services.AddSingleton<ExampleContext>(database);}

And make sure this is injected into ExampleController via the constructor:

Before we introduce Popcorn in depth, lets check that this all works, and demonstrate why we even need Popcorn in the first place.
Postman will help us here. Issue a ‘GET’ to ‘/api/example/employees’

We have all our data here! Great! Our entire heirarchy has been returned to us.
But… what if we didn’t actually care about the vehicles? That could be a ton of information that we don’t need, using extra data and time.
So let’s add Popcorn and see how it makes us more efficient.

If you haven’t already, now would be a great time to visit the Quick Start Guide and make sure you’ve got Popcorn linked into
your project correctly.

To utilize Popcorn we need to do three things (in the future, only one, so come back when v2 is released!).
Add two ‘projections’, which are the data types that format our outgoing data. A projection is simply a class that reflects what an entity
should look like before being sent to a client. Check out this answer on stack overflow for a better description.
You may be more familiar with the name DTO, Data Transfer Object, we simply prefer pronounceable names
over acronyms.

Okay, so you may be wondering why we even need these. In the future we hope to skip projections where there’s no advanced setup needed, but for now two things:

We want to make all non-nullable types nullable, so they don’t need to be included if not desired.

We have to ‘whitelist’ properties. Any property not on the destination type will not be included, even if explicitly requested.
So if you have a property called ‘SuperSecretEncryptionKeyNeverShare’, you can exclude it from the projection and be sure that no Api client will see it.

For advanced setup, as you’ll see in later tutorials, projections give you a lot of control.

Finally, we need to activate Popcorn on our pipeline and let it know about the projections. Back in Startup.cs:

// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){vardatabase=CreateExampleDatabase();services.AddSingleton<ExampleContext>(database);// Add framework services.services.AddMvc((mvcOptions)=>{mvcOptions.UsePopcorn((popcornConfig)=>{popcornConfig.Map<Employee,EmployeeProjection>().Map<Car,CarProjection>();});});}

We let the MVC pipeline know we want to use Popcorn, then configure Popcorn with two mappings, Employee -> EmployeeProjection and Car -> CarProjection.
Again, in the future we will have features to auto-detect simple mappings like these or obviate the need for a projection at all.

To demonstrate, first call the same endpoint ‘/api/example/employees’ and verify it looks the same:

A few things to point out: The casing has changed due to our internal popcorn settings (FirstName instead of firstName).
Also, our Colors enumeration has gone from a numeric format (1,5) to textual (Red, Yellow), but this can all be configured to your tastes.

To start seeing the real power of Popcorn, lets make a couple of advanced calls. Maybe we only want our Employee’s names?
Make a GET request to ‘/api/example/employees?include=[FirstName,LastName]’: