NorthwindTraders
NorthwindTraders copied to clipboard
[Question] External API implementation
Hello,
Once again, thank you for maintaining this project up to date and being open to discussion and questions. I checked out #135 but I am still a little bit confused.
Do you plan on adding some kind of "fake" API service to Northwind Traders somewhere?
For example, if I add an API Client project, built with Flurl for simplicity. This client needs an API key. I am wondering how to properly integrate this client inside my ASP.NET Core 3.0 application following your architecture, how do I pass along the API key, how do I call the client from the Commands/Queries without creating a new instance every time? Should I create a new instance every time?
I feel a little bit confused, I am going to read more about dependency injection but I must say an example would help, do you know of any resources that could be helpful?
I hope this question doesn't "pollute" the repo and can be taken into considerations. Thanks!
Do you have a sample project that you are struggling with?
This client needs an API key.
It really depends where you are storing this API key. Let's say you are storing it in the appsettings.json. Take a look at DependencyInjection.cs in Persistence. Here we are getting the Connection String, you could get your API key the same way.
how do I call the client from the Commands/Queries without creating a new instance every time?
The first part of the question is answered by @JasonGT in #135 .
1. Add an interface and supporting types to Application.
2. Add an implementation and mappings to Infrastructure.
Example for 1. INotificationService.cs Example for 2. NotificationService.cs in Infrastructure
Should I create a new instance every time?
If you are using .NET Cores DI, read about the 3 different scopes. Singleton, Scoped, Transient. Official Documentation
Don't be afraid to try out all 3 and see what happens.
Thank you for your answers.
Since my initial question I read more about DI, options and configuration and it totally goes in the direction you are suggesting.
I am still a little bit confused as to how reuse this code properly. For example, I'd like to create a separate APIClient project, so I can use it in Northwind Traders but also somewhere else. In that scenario, how does this part falls into place?
- Add an interface and supporting types to Application.
- Add an implementation and mappings to Infrastructure.
I am going to try and have a sample project ready as soon as possible, it will be way easier to explain.
Thank again,
Absolutely, that's a great idea. I would probably treat the APIClient as its own project and just reference it from the infrastructure project.
1. Add an interface and supporting types to Application.
2. Add an implementation and mappings to Infrastructure.
This is still applicable. Follow INotificationService/NotificationService. In NotifiactionService.cs you would initialize and call your APIClients method and maybe transform the Response to a Application Layer DTO and nothing else potentially, but that's okay.
I have a follow-up question to this which did not warrant the creation of a new issue in my opinion.
If the external service already provides an implementation which you can include, would you just use that in the application layer, or wrap it again and proxy it in the infrastructure layer?
For example, a SOAP service. If you discover it from within Visual Studio, the entire service is generated and easy interfaces are already provided. Or, you can use NSwag to create a C# client implementation for a REST API and expose that as a NuGet package to consumers.
On one hand, since both provide an interface, it's already somehow shielded and DI ready and you could argue that it's just another infrastructure a-like implementation. On the other hand, the actual implementation also resides hidden in the application layer. However, if you were to move it to the infrastructure layer, you potentially need to create a lot of DTO's and proxy implementations just for the sake of separation.
Thank you for your interest in this project. This repository has been archived and is no longer actively maintained or supported. We appreciate your understanding. Feel free to explore the codebase and adapt it to your own needs if it serves as a useful reference. If you have any further questions or concerns, please refer to the README for more information.