I am still new to ASP.NET MVC world and still try to learn best practices and best architecture patterns. Here comes one the things where i can't decide on my own and need community support:
I am not sure where I should put all of my business logic and how much of it could be in the controller. I know best would be if ALL business logic is implemented in my service layer. One of the biggest concerns I have is that I want to show not only simple model errors in the view I also want to show error messages in different places when more complex business validation was made. But when I have a service layer which does the full validation and that is completely decoupled from web layer (controller, views) then it makes more complex passing all the errors and assign to the correct view properties.
Which of the following options make more sense or is there no general recommendation? Just high level examples. no correct syntax or naming :-)
Option 1:
Controller.Action(){
if(ModelState.IsValid){
ServiceLayer.IsAllowedMoreComplexCheck();
ServiceLayer.IsBusinessModelValidCheck1();
ServiceLayer.IsBusinessModelValidCheck2();
ServiceLayer.IsBusinessModelValidCheck3();
ServiceLayer.DoAction1();
ServiceLayer.DoAction2();
ServiceLayer.DoAction3();
ServiceLayer.SaveChanges();
}
}
Option 2:
Controller.Action(){
if(ModelState.IsValid){
ServiceLayer.IsAllowedMoreComplexCheck();
ServiceLayer.DoAllActions();
}
}
ServiceLayer.DoAllActions(){
ServiceLayer.IsBusinessModelValidCheck1();
ServiceLayer.IsBusinessModelValidCheck2();
ServiceLayer.IsBusinessModelValidCheck3();
ServiceLayer.DoAction1();
ServiceLayer.DoAction2();
ServiceLayer.DoAction3();
ServiceLayer.SaveChanges();
}
Option 3:
Controller.Action(){
if(ModelState.IsValid){
ServiceLayer.IsAllowedMoreComplexCheck();
ServiceLayer.IsBusinessModelValidAllChecks();
ServiceLayer.DoAllActions();
}
}
ServiceLayer.DoAllActions(){
ServiceLayer.DoAction1();
ServiceLayer.DoAction2();
ServiceLayer.DoAction3();
ServiceLayer.SaveChanges();
}
there are also few more options... but just to indicate what i am thinking about.
Every feedback is welcome.
Thanks!!! Simon