Search code examples
c#apientitydbcontext

Registering DbContext in API Gateway


Using Ocelot I have created an API Gateway. Sitting behind this API Gateway I have 2 other microservices which require the ability to do simple CRUD transactions. These services are:

  • Customer.Api
  • Identity.Api

I have a separate DAL which contains a DbContext, entitites, repository and services. My plan is to inject the interfaces for these data access services into Customer.Api or Identity.Api so they can be used by the controllers.

However, should the DbContext be added on the Gateway or would it require to be added to both services (Customer and Identity) in order to use the DAL repositories?

If the DbContext needs to be added on every microservice wouldn't this be considered overkill?


Solution

  • In my eye, an API gateway should be just that... a configurable access point to other services. Really no logic or understanding of app domain other than forwarding along the request/response.

    Your services should implement any sort of context they need. Yeah, you might feel cumbersome to add the same context to your apps. But that would be the beauty of the microservice... it can work by itself without assistance.