ServiceLayer or Controller?

I have a gallery in my project. I save images to my hard drive, but tags, descriptions, etc. I am saving to a database. Working with the database and data validation occurs through the service level. As the user deletes the image, the files will be deleted from the hard drive and the entry will be deleted from the database.

//Action 
public ActionResult Delete (int id) 
( 
var entity = ServiceLayer.Entities.Get (id); 

System.IO.File.Delete (entity.FileName); //Might it be also be put to the service layer?
ServiceLayer.Entities.Delete (entit); 

return RedirectToAction ( "Index"); 
) 

      

Is it better to expose code to delete files at the service level or controller?

+2


source to share


4 answers


You should always put this type of code in the service.

The controller should do as little as possible - it should know how to get the service, pass any parameters to it, and return a view.



While the code is simple nowadays, it can grow over time, which is a good way to provide a good structure to start with.

+3


source


Better to do this at the service level.



The controller should only handle what the user requested, calling the required services to do what the user wanted and showing the user what they should see in response.

+2


source


I would post this at the service layer so you can abstract and test it with TDD.

0


source


You need to ask yourself the question, what is my dispatcher responsible for?

IMHO, controllers should act as intermediaries between View and Model, where Model in this case includes Services.

However, there are never strict rules about where and why. In contrast to the fanatics posting strong answers, I took a pragmatic approach. Would adding a filesystem to your database be more useful? Whether this is the result of a type explosion - your service will need to have a dependency on the filesystem: addet == +1 interface +1 concrete implementation +1 test class in case of unit tests, etc.

It's not worth it, so use your judgment.

0


source







All Articles