Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
They say controllers should be thin and should not contain buisness logic , Well I was trying to implement the thin controller.
But suppose I have existing code like ,

C#
public class HomeController : BaseController
{
    public ActionResult HomeCapture(HomeModel model)
        {
            if (model.xprop == 1) {
                return RedirectToAction("ABC", "Help");
            } else if(model.yprop == 2){
                return RedirectToAction("Index", "Home");
            }else 
			{
				return RedirectToAction("Error", "Exit");
			}
			
        }
}


to make this controller thin , I am planning to move this code to another class(eg: HomeDecorator) and call that method from controller
But how am I supposed to write RedirectToAction in that class ? as it can only be written in controller.
is it ok if I take new RedirectToRouteResult in that class(HomeDecorator) and return it to controller?
Please help ...
Posted
Updated 16-Sep-15 7:03am
v2

That code isn't business logic, it is code that deals with the inputs from the view and handles what is output from the controller so it should stay in the controller. When people say you should have business logic they mean code that governs the rules of how your site works. So if your site was a hotel management site your controllers should have code that checks the availability on a given date then uses the results to make decisions such as what rate the room is charged at. Instead your controller should handle the inputs such as the date and location, then pass those onto a model (or other class) that does all the business logic then returns to the controller a list of data and that data is all the available rooms which the controller then Returns in a relevant way.
 
Share this answer
 
Comments
Black Mamba Elapidae 20-Sep-15 23:50pm    
Thanks for your answer,But I have posted simplified version of my code.. In every loop my actual code is also doing some other things with redirect to action. and I cant just remove that much code and move it to business logic because it comes with the redirect to action at the end of the loop..
So my question is what do you recommend , Should I keep all that code there in the controller ? Or call different business logic function in every loop ? Or use the approach I have mentioned in the question???
F-ES Sitecore 22-Sep-15 4:17am    
Have your function return some kind of status, probably using an enum, and the controller will call the code and redirect to different actions depending on what the returned enum is.
Black Mamba Elapidae 22-Sep-15 6:01am    
Thanks a lot !! I will follow your approach , Please correct me if I am wrong,
With the status(whether it is returning RedirectToAction/json/View/Partial View)
I will also need different data(as for returning View I will need Viewname and model name, for redirect to action I will need controller name action method name etc)
I will be creating a class which will hold a status and respective data like:
Status = RedirectToAction/json/View/Partial View
and Different data depending on status,
- My buisness logic function will return the object of above mentioned class
- and depending on the status of that object and data I will redirect from controller
F-ES Sitecore 22-Sep-15 6:51am    
No I wouldn't have the business logic return info about views\actions etc. Only the controller should know about those things. Your business layer should handle business logic and business logic only. Let's say you want a winforms version of your site and you want to re-use your existing business logic. If your business layer is returning actions and views etc, then how is that usable from winforms? If your business layer simply returns a status, it is up to your presentation layer to decide how that status is actioned. So your mvc site will direct to a view or action, the winforms site will show a form. So you'll have code in the controller like

var status = myBusinessLayer.TakePayment(cc, amount);
switch (status)
{
case Success:
Redirect("Success", "Payment");
case Rejected:
Redirect("Failure", "Payment");
case Error:
Redirect("Index", "Error");
}
Black Mamba Elapidae 22-Sep-15 8:10am    
Thanks for explaining it nicely with example :)
If your code is not thin then every other code that every developer has written is very thick. In other words, your code cannot get thinner than what you already have.
 
Share this answer
 
Comments
Krunal Rohit 17-Sep-15 1:11am    
5!
-KR
Black Mamba Elapidae 20-Sep-15 23:52pm    
Hey thanks for your answer, Code I have posted in the question is just the pattern of my controllers. Please refer comment I have posted on F-ES Sitecore's answer below , and Please suggest me the right approach

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900