Skip to content

Latest commit

 

History

History
48 lines (40 loc) · 2.68 KB

File metadata and controls

48 lines (40 loc) · 2.68 KB

Popcorn > Documentation > DotNet > Tutorial: Setting Contexts

⚠️ v7-only — dropped in v8. SetContext(Dictionary<string, object>) does not exist in v8. The entire "pass an ambient dictionary to your lambdas" pattern is superseded by standard ASP.NET Core dependency injection:

builder.Services.AddSingleton<ExampleContext>(database);

app.MapGet("/cars", (IPopcornAccessor access, ExampleContext db) =>
    access.CreateResponse(db.Cars));

Translator lambdas that needed ambient data in v7 become endpoint-side resolution in v8 (see MigrationV7toV8.md §5).

The tutorial below is preserved for v7 users still on that line.

Popcorn does not know about everything in your application on its own. Actually, it's aware of virtually nothing without being specifically told and thus we use .NET MVC options with the Popcorn Configuration as an abstraction layer between whatever it is you're doing on your back end and your API.

Enter the need for the ability to give context to Popcorn so it has access to the necessary bits and pieces to serve your API appropriately.

Usage

This is actually extremely simple in explanation, but very complex in the way it can be used. All you have to do is declare "SetContext" as seen below and in Startup.cs with a Dictionary<string, object> of all the contexts you'd like popcorn to be aware of. The generic object usage means that virtually anything can be passed in as a context.

services.AddMvc((mvcOptions) =>
{
    mvcOptions.UsePopcorn((popcornConfig) => {
        popcornConfig
            .SetContext(new Dictionary<string, object>
            {
                ["database"] = database,
                ["defaultEmployment"] = EmploymentType.Employed,
                ["activeUser"] = userContext.user
            })

We don't limit your options on what you can use as a context beyond requiring the contexts be entered in a Dictionary format (enter the complexity) because contexts will look quite different from project to project.

You'll see in our tutorials on Authorizers and Advanced Projections that we give a few trivial examples on how you can set some contexts to be used within Popcorn itself. Authorizers uses a context to see what the "active user" is for each request, while Advanced Projections shows how a default Employment context can be used with factories. We also shouldn't forget the obvious that you'll probably need to make your database accessible to Popcorn as a context!