Monday, May 11, 2009

Poor man’s MVC

Hi, I’m considering using a what I call a poor man’s MVC for a project at work.  Actually the more I think about it, the more I think I’m going to use Fubu, so I figured I’d blog this out before I forgot about it.

The idea is that if you are in a medieval torture chamber and are being forced to use WebForms to write an app you could do the following.

Let’s say you have a page with the following event’s on it. 

Load()

ApplyThumbscrew()

ListenToScreams()

the Load you surely recognize as one of the god awful page life cycle events. ApplyThumScrew() and ListenToScreams() are obviously ways that a user can enjoy ones self.  They click a button and said event happens.

For each page you will have a corresponding controller ( or presenter if you will.  I’d rather not. ).  The Controller will have corresponding methods to the Page.  It will also have a specific input and output.  The sig will look something like this.

public ApplyThumbscrewOutputModel ApplyThumbscrew(ApplyThumbscrewInputModel input) { }

the input and output models are just DTOs. The input model has everything form the page that you need to do you stuff.  the output model has everything that the page needs from the server to do it’s stuff.

so the ApplyThumbScrew() event would look like this

but wait first let me tell you that you should be using Dependency Injection for the controller ( and later the torture object ) and a proper IOC Container.  But that is beyond the scope of this blog and would require that I write more code in this horrible editor.

protected void ApplyThumbscrew(blah blah blah)

{

var input new ApplyThumbScrew{

SizeOfThumb = txtSizeOfThumb.Text,          

HowBadIsPerson = txtHowBad.Text,                          

HowSickAreYou = txtHowSick.Text,           

                                           };

var controller = new TortureController();

var output = controller.ApplyThumbScrew(input);

// then do something with return. my imagination is waning

txtPersonsThumb.text = output.PainExpression;

txtUsersEnjoyment.text = output.Enjoyment;

}

Then the corresponding Controller action would look like so

public ApplyThumbscrewOutputModel ApplyThumbscrew(ApplyThumbscrewInputModel input)

{

var torture = new InterigationTechnique();

var fun = torture.DoTheScrew(input.SizeOfThumb,input.HowBadIsPerson,input.HowSickAreYou);

return new ApplyThumbscrewOutputModel{

PainExpression=fun.Pain;

Enjoyment = fun.Enjoyment;

                                                        };

}

 

So you get the idea.  coding like this sucks and the formatting is all fubar so no more code.

But the idea is that now you have the “codebehind” doing nothing but view related stuff.  I harvests information gives it to a more responsible agent then receives some goo to spread back on the view.  In so doing you have completely severed the nasty httpcontext and Request object and all that untestable shite from the business logic.  Rendering the business code, yep you guessed it, TESTABLE!  yay.  furthermore, if you ever are released from the dungeon, you will have code that you can then hook into any view mechanism you want.  Fubu, Asp.Net, some other shite.

I don’t understand what is going on with the formatting.  this is bullocks.  Anyway, sorry that this is kind of superficial but really you should be using a proper MVC framework anyway, not this webforms crap.

1 comment:

Chad Myers said...

lol. A good bunch of posts lately. Keep up the good work!