I see from the sample applications how to add a controller method to the HttpApi project (TestController)
How do you use dependency injection to get access to the DbContext?
Actually, I have 2 DbContexts I want to inject into my HttpApi controller, so I can perform database read/writes.
4 Answer(s)
-
0
Hello @robb,
Directly injecting the DbContexts will violate the Seperation of Concern principle. As much as it would seem to be a fast way to get things moving; it will cause more problems in the future.
Suggested way is to move this logic to application layer. You can inject different repositories from different dbcontexts and handle it on application layer. Then inject the application layer to Api or wherever else you want to.
I hope it was helpful.
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
0
gterdem's answer is absolutely right.
However , if you want get dbcontext, you can see the docuemnt : https://docs.abp.io/en/abp/latest/Entity-Framework-Core#access-to-the-ef-core-api
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
0
Hello, it is pretty straightforward; here is a link from abp-sample repository BookStore sample injecting appService into api controller.
Keep on mind, when you implement your appService, you will already have api endpoints thanks to dynamic proxy. So you may not need to add anything to HttpApi. See aslo Auto API Controllers.
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)