Skip to content

Decide what to do if we need context from services but it's registered under a base-type #732

Closed as not planned

Description

@rowanmiller

There are going to be several places where we try and obtain a context from DI (migrations, database error page, and maybe others).

This is fine if the context is registered under it's exact type but gets harder if it's registered under something different (interface, base type, etc.).

In the error page it just fails in these scenarios at the moment, but I think we should do something central and reuse it across the product. We could try the exact type, then walk up the inheritance chain.

Activity

  1. rowanmiller commented on Oct 1, 2014

    @rowanmiller
    ContributorAuthor

    @bricelam not sure if you have thought about this one at all?

  2. bricelam commented on Oct 2, 2014

    @bricelam
    Contributor

    Seems fine to walk up the chain and cast to the specific type, ignoring it if it's not (i.e. use as)

    This feels like it should be part of #639.

    The process for getting the instance would go something like this:

    1. Use IContextFactory (or similar) if implemented
    2. Get it from the services
    3. Use a well-known constructor (injecting services if needed)

    The process to get the services could go something like this:

    1. Pass them to the DbContextInfo-like class
    2. Use IServicesFactory (or similar) if implemented
    3. Build them by convention (e.g. using Startup.Configure() for web apps)
  3. modified the milestones: , Backlog on Oct 8, 2014
  4. bricelam commented on Oct 8, 2015

    @bricelam
    Contributor

    AddDbContext<TContext>() registers an instance of IDbContextFactory<TContext> under IDbContextFactory and the TContext.

    Commands take advantage of this by...

    1. Asking for IEnumerable<IDbContextFactory>
    2. Looking at the TContext generic argument
    3. Asking for TContext
  5. ajcvickers commented on Jul 8, 2016

    @ajcvickers
    Contributor

    I don't think we need to do anything more on this at the moment. Our mechanisms for using AddDbContext and discovering contexts seem to work okay.

  6. removed this from the Backlog milestone on Jul 8, 2016
  7. rowanmiller commented on Jul 8, 2016

    @rowanmiller
    ContributorAuthor

    👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions