C
C#2y ago
Hercules

What am I doing wrong in Clean Architecture?

I got this .sln with seperation of concerns and i have an API that is supposed to fetch string data from any link. The chain I'm working on is the Contract (Models for request and responses) --> Application (Where the logic resides) --> API (Controller/where I invoke the url to get the data). Contracts - Domain layer Models that is reuse.
public record TextRequest(List<string> List);
public record TextResponse(List<string> List);
public record TextRequest(List<string> List);
public record TextResponse(List<string> List);
Application Layer- my business logic for getting data (http request) accepting multiple links in a List<string>();
public interface ITextService
{
Task<List<string>> ReadTextAsync(List<string> sourcePath);
Task<string> HelloWorld();
}

public class TextService : ITextService
{
private readonly HttpClient _httpClient;
public TextService(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<List<string>> ReadTextAsync(List<string> sourcePath)
{
return await ProcessRepositoriesAsync(_httpClient, sourcePath);
}

private static async Task<List<string>> ProcessRepositoriesAsync(HttpClient client, List<string> sourcePath)
{
var listOfTexts = new List<string>();
foreach (var link in sourcePath)
{
var source = await client.GetStringAsync(link);
listOfTexts.Add(source);
}
return listOfTexts;
}
public interface ITextService
{
Task<List<string>> ReadTextAsync(List<string> sourcePath);
Task<string> HelloWorld();
}

public class TextService : ITextService
{
private readonly HttpClient _httpClient;
public TextService(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<List<string>> ReadTextAsync(List<string> sourcePath)
{
return await ProcessRepositoriesAsync(_httpClient, sourcePath);
}

private static async Task<List<string>> ProcessRepositoriesAsync(HttpClient client, List<string> sourcePath)
{
var listOfTexts = new List<string>();
foreach (var link in sourcePath)
{
var source = await client.GetStringAsync(link);
listOfTexts.Add(source);
}
return listOfTexts;
}
Api - Presentation Layer Where I expose the Url : /text/getText
[ApiController]
[Route("text")]
public class TextController : ControllerBase
{
private readonly ITextService _textService;

public TextController(ITextService textService)
{
_textService = textService;
}

[HttpGet("gettext")]
public IActionResult GetText(TextRequest request)
{
var textResult =
_textService.ReadTextAsync(request.List);
var response = new TextResponse(textResult.Result);
return Ok(response);
}
[ApiController]
[Route("text")]
public class TextController : ControllerBase
{
private readonly ITextService _textService;

public TextController(ITextService textService)
{
_textService = textService;
}

[HttpGet("gettext")]
public IActionResult GetText(TextRequest request)
{
var textResult =
_textService.ReadTextAsync(request.List);
var response = new TextResponse(textResult.Result);
return Ok(response);
}
1 Reply
Anton
Anton2y ago
there isn't right or wrong here really as long as you can test it and modify it easily, it's good enough