3

In my application, I have to get data from our ERP. For this example, let's say product data.

Some of the data will come from the API that our ERP has provided us, whilst some of it will come from direct DB calls as the API doesn't give us all the required detail.

For this situation, do I just do with a single Repository and within the repository methods I get the data from the required place?

If I do this, my concern is that I will be injecting into the constructor IApiService as well as database connection strings which depending on the data I am retrieving, it may not be necessary.

Do I create 2 separate repository, something like ApiProductRepository and DatabaseProductRepository?

Some guidance on the best methods here would be great!

3

I would create two separate implementations of the repository interface like ApiProductRepository and DatabaseProductRepository. Each implementation will throw an NotSupportedException for methods that it doesn't support. Finally create a ErpRepository that takes an ApiProductRepository and DatabaseProductRepository and invokes the appropriate implementation based on the method.

Here is a class diagram describing how I would design the solution

enter image description here

Edit: I was answering under the assumption that some methods would be provided by the DB and some by the API. After OP clarified that some attributes of the model will be filled from the API and some will be filled by the DB, the design above doesn't cover that case well, and I would use the simpler design:

enter image description here

  • I like it! Will implement and see how I go – Lock Jul 18 '17 at 4:55
  • Why the downvote? – Samuel Jul 18 '17 at 6:10
  • 1
    +1 for using PlantUML. So great tool. However, the last composition (IMO) is unnecessary. Isn't it? – Laiv Jul 18 '17 at 7:37
  • 1
    @Laiv: IMHO< the composition is useful to hide for higher-level clients the fact that the Product information is coming from multiple sources. – Bart van Ingen Schenau Jul 18 '17 at 10:14
  • 1
    Thanks @Samuel. So I would pass in a connectionString as well as a IApiService so I can get data from both sources and merge. Does this sound OK? – Lock Jul 18 '17 at 23:14

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.