2

We have kind of an interesting situation here. We are using the repository pattern with Entity Framework so each database table has its own Repository class that accepts an instance of a DbContext in its constructor. We are also using Ninject for dependency injection. We have defined a single context to be instantiated during a given request so that when a multiple of repositories ask for a DbContext, the same instance is used throughout. This allows us to follow a UnitOfWork pattern so multiple things can occur on multiple repositories and a single commit will commit all changes to the database.

Here is the issue, we are also using SQL Azure Federations which splits up our client data into multiple databases (sharding). We need to be able to jump from one federation member (database) to another within the same request but want to be able to use the same service/repository methods that are dependent upon the injected context. Our first thought was to just execute the USE FEDERATION sql command on the existing context to move to the next database but it seems to work sometimes and not others. After executing the statement we see that the underlying connection on the context really is pointing to the new server but for some reason queries executed on that context end up returning results from the previous connection. I presume using the same instance of a context on multiple database is not something that is really natively supported as you would normally spin up a new context when connecting to a new database. Unfortunately we have a bunch of repositories that are created dynamically using Ninject which then take in the same instance of the context so even if we do spin up a new context for the new database, we have no way to making all the existing repositories suddenly become dependent upon the new context we just spun up instead of the one that was created at the initial request.

Here are a few solutions we can think of but are not sure how to get any of them to work:

  1. Change the database used on an existing context (as explained above but didn't seem to work)
  2. Swap out an injected context for all repositories with a new one so that all existing repositories now become dependent upon a different context
  3. Somehow request from Ninject a new instance of all the repositories passing in the parameters needed for the context once it is requested.

Again, bottom line here is we have a set of repositories and services that are dependent upon a single Context and we want to be able to reuse those services and repositories but swap out or change the context, on which all are dependent, to point to a new server.

Nick Olsen
  • 6,299
  • 11
  • 53
  • 75
  • 1
    I'd try and keep Ninject trickery out of this. Sort out what can/needs be done on one DbContext and do that. Then do the next bit. Is this all happening in the middle of a single Request? How are you managing retries? – Ruben Bartelink May 01 '13 at 20:00
  • For scenarios like this we usually don't use Ninject but it just so happens that this specific thing we are doing is dependent upon 10 to 15 other respositories/services which then have their own dependencies. We were trying to avoid have to instantiate all of these ourselves as Ninject can do it for us. – Nick Olsen May 01 '13 at 20:33
  • So, what's going on - in response to a what type of Request in what type of app? Are the contexts long lived? Is it unimaginable that you might split the database? Are those 10-15 repositories really all part of a single unit of work? You need to explain more about what's intrinsic to what you're doing. I suspect that you'll find, as part of explaining that, you'll end up with clear batches of work to do within which you won't need to go 'changing contexts' and getting into these puzzles. – Ruben Bartelink May 01 '13 at 21:15

1 Answers1

0

Solution was to just completely drop Ninject out of the scenario. Not the best solution but the tools we are using were not really designed to do what we wanted in the environment we are working.

Nick Olsen
  • 6,299
  • 11
  • 53
  • 75