Reputation: 60065
This question is a bit rhetorical. At some point i got a feeling that ASP.NET MVC is not that authentic implementation of MVC pattern. Or i didn't understood it.
Consider following domain: electric bulb, switch and motion detector. They are connected together and when you enter the room motion detector switches on the bulb. If i want to represent them as MVC:
Switch has one private field (On/Off) as a State and two methods (PressOn, PressOff). If you call PressOn when it is Off it goes to On, if you call it again state doesn't change.
Bulb can be replaced with buzzer, motion detector with timer or button, but the model still represent the same logic. Eventually system will have same behavior.
This is how i understand classical MVC decomposition, please correct me if i am wrong.
Now let's decompose it in ASP.Net MVC way.
So the logic that defines behavior moves to controller.
Question 1: Is my understanding of MVC and ASP.NET MVC correct?
Question 2: If yes, do you agree that ASP.NET MVC is not 100% accurate implementation?
And back to life. The final question is how to separate model from controller in case of ASP.NET MVC. There can be two extremes. Controller does basic stuff and call model to do all the logic. Another is controller does all the logic and model is just something like class with properties that is mapped to DB.
Question 3: Where should i draw the line between this extremes? How to balance?
Thanks, Andrey
Upvotes: 5
Views: 660
Reputation: 1
In MVC framework - Request always FIRST comes to Controller; then only model created and rendered using view
In your case request going to Switch (Which is model as you said), so you cant decompose the switch-bulb-motion scenario in way mentioned.
Upvotes: 0
Reputation: 39807
I think it could be either way. Here would be an implementation is ASP.NET MVC that keeps the logic like you had in your first example.
Model (Respository)
Function switchOn() as bulb
if !bulb.lightOn then
bulb.lightOn = true
end if
return bulb
End Function
Function switchOff() as bulb
if bulb.lightOn then
bulb.lightOn = false
end if
return bulb
End Function
Function Motion(senseMotion as boolean) as bulb
if(senseMotion and !bulb.lightOn) then
bulb.lightOn = true
end if
return bulb
End Function
Controller
Function PressSwitchOn() as actionresult
return view("Bulb", lightRepository.switchOn)
End Function
Function PressSwitchOff() as actionresult
return view("Bulb", lightRepository.switchOff)
End Function
Function SomethingMoved(byval hasMoved as boolean) as actionresult
return view("Bulb", lightRepository.Motion(hasMoved))
End Function
No business logic in my controller, it is simply passing the state from the model to the view. Am I in the ballpark of your example?
To answer questions.
Upvotes: 2
Reputation: 60574
In your example, the motion detector still has to call SwitchOn and SwitchOff, which is basically the same as a controller calling the same methods on a repository. So if you consider an ASP.NET MVC application implementing the repository pattern, your argument falls.
Conclusion:
Upvotes: 1