0

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

Guanxi
  • 3,103
  • 21
  • 38
Simon
  • 171
  • 2
  • 11

2 Answers2

0

As per my experience keep your business logic in service layer because I think by this way you keep the loose coupling motto of MVC intact. Yes it's true you need some extra code to handle validation and error but I think for a good enterprise level application you must design good architecture that must survive for a long time.

Best place to play with MVC is play with its pipeline and provide your custom logic for pipeline components.

For simple implementation you can do following things:-

You can create custom action attributes to do validation.

For error handling you can create custom error action attribute and provide your own OnException() method implementation by implementing IExceptionFilter interface in your custom error.

Refer this link for error handling in detail.

Guanxi
  • 3,103
  • 21
  • 38
Vikas Gupta
  • 188
  • 1
  • 7
0

I am not sure where I should put all of my business logic and how much of it could be in the controller.

No business logic should be in controller. Now, what do you mean by putting in Service layer. Service layer should only handle purpose of servicing, Business logic should be in Business logic layer and should be independent of service layer too (what if you want to make changes to the way your service layer serve?).

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.

In long run that will pay off. Error handling, exception handling should be thoroughly thought of when developing an application.

Guanxi
  • 3,103
  • 21
  • 38