I've been using the SOLID Design Principles, in C# with Entity Framework for a while, and works great. Now I have a situation where I need to get data from an external Web API call. What's the best way or what approach should I take to integrate an external data source into my solution. I have the model objects I need, but not sure how to modify the Persistence layer to call and get the external data. Thanks for the help and comments.
Asked
Active
Viewed 143 times
1
-
How do you wrap the the EF access today? – Magnus Sep 15 '22 at 06:57
-
I'm implementing such services at the same level called infrastructure where both projects live (persistence, external service). Contract lives at the core of my application. – T. Dominik Sep 15 '22 at 08:37
1 Answers
2
I would define an interface IExternalDataService
(although you should find a more suitable name for it, for example, if it fetches users from an external API I would call it IExternalUsersService
) that my PersistenceLayer
depends on. A concrete implementation will be injected in the PersistenceLayer
on instantiation.
interface IExternalDataService
{
Task<IEnumerable<Data>> FetchAsync();
}
class ExternalDataService: IExternalDataService
{
// here comes the implementation
}
class PersistenceLayer
{
// ...other properties and methods
private readonly IExternalDataService _externalService;
constructor PersistenceLayer(IExternalDataService service)
{
_externalService = service;
}
public async Task<IEnumerable<Data>> GetAll()
{
return await _externalService.FetchAsync();
}
}
// somewhere else
var persistenceLayer = new PersistenceLayer(new ExternalDataService());