I’d like to say I’m a strong believer in MVC, but I’ve never quite gotten it to work cleanly for me. There are two problems it doesn’t solve for me, and those are the two that I’m looking for my pattern to solve.
First, there’s the issue of what to model in the Model. Do I model the back-end data/services/objects or the UI data model? Note that these are different. The UI model is inherently about what’s being displayed. The back-end model is about the inherent behavior of the back-end system. You can have 10 different UIs sitting on the same back-end. Separate, different.
Having services and UIs differ is a valid and good thing. Look at all the interesting mashups. Does this mean you should explicitly not model the UI in your backend? The Model is just the back-end model? Or does the Model actually live in the client, and I’ve got it all backwards?
I think the point of the Model is to model the UI, but being primarily a back-end guy, first and foremost I’d like to have clean APIs to my services. Someone smarter than me will create a better UI than the one I’ll ship out of the box.
Hmm. So I’m going to model the back-end, and with Laziness as one of my first order principles of good design, I’m damned if I’m going to have two different models, so I’m going to neglect the UI model. But if I don’t model the UI data, who will? Feels like I’m not doing MVC right.
Ok, so the Model has me conflicted. Perhaps the View will clear it all up for me.
What is the View, exactly? In the Web world I tend to think of the View as the HTML. I think the point is to have the model be free of excess code, a nice pristine representation of the UI I can hand to an HTML guy to make pretty. Is this right?
But Views never are clean of code. They’re messy. They’re gooey. That’s their nature. Views change based on the state of the app, the nature of the data, etc. You must be able to express if/then/else, calculate things, do all sorts of messy nonsense. Perhaps someone smarter than me can do that cleanly, but I always end up with code and presentation all together.
Ok, so where am I now? I’m not sure what to Model, and my View is getting messy.
Well, the controller I’m ok with. I understand what that is. That controls state.
Because of all this, I can never get MVC quite right. I end up in PHP land, embracing the messiness. Throw the code into the template, mix in some of the controller in there too even though I know I shouldn’t. The MVC way won’t be clean for me anyway, so let me at least get the job done quickly. PHP is great at that. I think Dave Megginson is expressing a similar sentiment here?
Recently I’ve been playing around with AJAX, and things are becoming clearer.
My Model will be my services model, not my UI model. The two are very separate in very clear ways: they’re written in different languages in different places, one on the back-end, one on the browser. No problems there.
My View will be my code in the browser. It’s messy. It’s ugly. I really don’t like Javascript. But at least I know what goes there. The presentation code and the presentation are inexorably mixed together (the former manipulates and modifies the latter), and that’s just how it is, so I don’t worry about the cleanness. But it’s clean in that my interfaces to my back-end are well defined and not mixed in with my View.
How about the Controller? This one becomes a little more cloudy for me in practice, because I’ve separated out the View completely, so really I’m controlling my back-end state. Which is different from controlling my View state. It becomes more an issue of validating that the state as I know it on the back-end is the same as the state on the front-end. Or, more acurately, to validate that the state transitions the front-end requests are acceptable to the back-end.
There you have it. I’m on my third AJAX project / experiment, and I find I’m spending most of my time on the design of the APIs and the state transitions. That feels right. If I nail that, I can build a new UI completely independently. Better yet, someone else can do it. I know where all the code goes. Things feel clean.
I’m not really sure this is even the MVC anymore, or that I ever had MVC right, but it fits my brain better than other solutions I’ve tried before. And I’ve tried a few. Perhaps the problem is I’m applying MVC to the wrong part of the problem?
What do you think? Someone who understands MVC please set me straight.