-2

I have written a small application in ASP.NET Core to create and manage collections of cards for a collectable card game. I currently have a version that successfully downloads bulk card data via API call, parses the JSON response, and loads it into local SQL Server database. I then use the local data to add the cards to the collections, lookup prices, etc. As an academic exercise, I'm deliberately overcomplicating the design of this as if it were a large scale enterprise application since I really have no reason to build it other than to learn more about programming, so I'm wondering about best practices for something like this?

Currently I have the application broken into four projects: an API client for pulling the card data from the external API, a data access/domain layer using EF Core and SQL server, a service layer that orchestrates everything, and a Blazor Server UI. The main thing that I'm struggling with is that my service layer is dependent on both the API client and the DBContext, so I'm wondering if there's a good way to consolidate the dependencies, since the data from both sources are mapped to the same domain objects.

From what I've been reading, it seems like setting up a repository would be a good option and is common when there are multiple data sources being utilized. I have a 2nd version with repositories for accessing the local database but I'm not sure how to introduce the external API calls into this version. I think I could create separate implementations of the Card Repository Interface, one to access my local SQL database and another to access the external API but I'm not sure how the application would know when I need one or the other if I'm using dependency injection.

For example, periodically I want to check for updated card data from the external API and update my database with the new data, but for the most part I'll be reading the card data from the local database for managing the collections. Any advice on how to approach this? I can give code examples if needed. Thanks.

DogoPilot
  • 13
  • 3
  • 1
    You might want to check this site https://softwareengineering.stackexchange.com/ . It is much more related to designing an application from software perspective. – Tatranskymedved Nov 15 '20 at 07:45

2 Answers2

2

Your service layer should not be dependent on the API or DBContext, instead (as you have researched already) you would have a repository that accesses the data for your service layer. The calls to the database and to other external APIs should also go within your repositories, since your service layer should not know anything about how the data is accessed.

Your repositories should be separated by business objects, not necessarily tables themselves - however I would definitely separate each external 3rd party API into its own repository.

For a logistics company I worked at, I had implemented a dynamic design where I was able to create a well structured repository pattern which would pull data either from databases internal to our company (transportation, and accounting systems), or from 3rd party APIs (such as flight or freight data) and store queried or response data into either our SQL Server or Oracle databases. This isn't too hard to accomplish.

For the repositories that make calls to a 3rd party API, an important thing I learned was making sure to design a versatile pattern for the API request calls. This was done by creating a separate APIClient solution in which you can create the HttpClient, add Headers, Query Parameters vs POST/PUT/PATCH body, and the other items required for your different types of requests. Also you have to account for including logic flow to get authentication tokens for subsequent API calls.

Another thing to consider, which I also took advantage of was the webhooks that the 3rd party APIs offered, that way I didn't have to constantly poll the data to see what changed, instead they sent me the data when things changed on their side.

There are many ways to approach your pet project, hopefully I've given you some useful ideas.

EspressoBeans
  • 1,747
  • 12
  • 22
  • 1
    Here's a good resource with some options for bulk api operations: https://www.codementor.io/blog/batch-endpoints-6olbjay1hd. FYI, the contributors here are correct that despite your fairly concise and well-thought-out post, it is the wrong forum for this type of question. However many on this platform lack tact to guide newbies without being overbearing (I saw that your post in the correct forum got lit-up too, lol) – EspressoBeans Nov 15 '20 at 17:37
0

Try limiting the number of sources you are pulling from that requires the logic to execute. Could you combine your data? Try streamlining your logic. Check this link out. It's for scanning changes in an SQL server.

Check for changes to an SQL Server table?

Ivan
  • 399
  • 2
  • 5
  • 18