markoverflow markoverflow - 10 days ago 5
ASP.NET (C#) Question

Asp.net core separation of concern using service layer

I'm having a problem on what is the best approach to design my service layer and use them in my controller. Here is my concern.

Currently I'm using this to delete categories

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Delete(List<Guid> ids)
{
if(ids == null || ids.Count == 0)
return RedirectToAction("List");

_categoryService.DeleteCategories(_categoryService.GetCategoryByIds(ids));
_categoryService.SaveChanges();

return RedirectToAction("List");
}


my concern is should I just pass ids to
DeleteCategories
then call the
GetCategoryByIds
inside the
DeleteCategories
. And If I'm only going to delete 1 Category, is it better to add another method like
DeleteCategory
then in the controller check the length of the ids and if it is only 1, use
DeleteCategory
instead,

Answer

my concern is should I just pass ids to DeleteCategories then call the GetCategoryByIds inside the DeleteCategories.

Just pass the ID's to the DeleteCategories method. I wouldn't even bother calling GetCategoryByIds inside of it. There's no need to query the database for all the rest of the category information if you're just planning on deleting it.

And If I'm only going to delete 1 Category, is it better to add another method like DeleteCategory then in the controller check the length of the ids and if it is only 1, use DeleteCategory instead

I wouldn't bother with creating another method. You could just pass a list with one value in it. There's nothing a DeleteCategory method could do that you can't already do with DeleteCategories.