A brief foray into the world of ASPX and MVP
Whew!! This has been a long time since i managed to put pen to paper as it were and post anything here. I’ve been spending a few weeks away from Biztalk in the murky world of ASP.NET. Its been an exhausting time but a great learning experience and lots of fodder for a set of blog posts. So, whats a Biztalk guy doing with ASPX then?
Well basically once we got the Oracle adapter working somewhat decently and linked up the needed systems, we were tasked with adding some aspx forms to interact with a couple of the services. Having volunteered for the task back when it was originally envisaged as a one man project (featuring yours truly) I had to come good on my promise. It is only a small aspx system, 4-5 pages with a few grids and data displays linked to the two backend services.
I could have gone down the standard webforms route, but with our main portal being implemented with Model View Presenter, i decided that to keep things consistent I would have to use the MVP pattern. Couldnt choose MVC though cos its still all .NET 2.0 and MVC isnt RTM anyway so that was out. The two main resources that kickstarted my foray into the MVP world were Jean-Paul Boodhoo’s videos (MVP, TDD-1 and TDD-2) for DotNetRocks TV as well as his excellent article on the same subject for MSDN Magazine. I highly recommend watching the DNRTV videos and although the TDD ones pre-date the MVP show, its better to start with the MVP show cos JP had introduced MVP in the TDD videos and sparked an interest and that led to him doing an entire show on MVP. I also spent some time with the videos on Polymorphic Podcast – their Design Patterns Bootcamp- MVP series. All of these are excellent.
I found myself leaning more towards the Passive View approach that JPB favors rather than the Supervising Controller which is the approach shown in the Polymorphic Podcast set. It seemed to me, when i first saw the show, that keeping the view to a complete minimum was definitely the way to go. Ironically, over the course of the project i found that i ended up doing more of the Supervising Controller particularly because rendering the gridviews play a major part in the system and so theres a huge amount of code in the view. Not business logic, mind you, but lots of display logic, databinding and so on. I think the Passive View approach is rather hard to do well when you have a fair amount of databinding to do and i really dont think the presenter should know that the View is using a grid or any specific control as it would make them too tightly bound.
Since there isnt any business logic as such , I elected to use a DTO library to pass data through the view – presenter – serviceagent layers. The serviceagent wraps the two proxies of the webservices and maps their structure to the simpler DTOs. It took me sometime to get my head around the page lifecycle and the way databinding works with custom object collections. Basically the gridView doesnt work well with the MVP (at least, when using Object data sources) because in the webforms world, the view is king and takes control immediately. So if you specify an object data source, the grid will immediately call into it and try and get the data from wherever the object points to , but in the MVP, we want to push the data into the grid so the grid wont get anything by simply initialising the collection. So I ended up having to dynamically databind and before that to serialize the collection into a DataView so that paging and sorting the grids became simpler. I didnt want to implement all that kind of paging and sorting logic into my DTOs. I read David Haydens article on using the WCSF Object Container Data Source when using grids with the MVP approach but by then i had got the dataviews to work so i didnt bother trying that out. Will file that away for future reference.
I can see why people struggle with the webforms model and why the MVP and MVC approaches are gaining so much popularity. Declarative stuff is alright but only as long as you buy into it wholesale and are willing to lose some control over the way the system works. Yes, it works for lots of systems, but personally i prefer to take more control over my system and the MVP gives me that. Im quite looking forward to trying my hand at MVC (only a little bit).
One thing my experience with the gridView and the hours i spent trying to style it exactly to match the mockup screens that formed the spec taught me was that CSS and layout are not my cup of tea. I still havent got the pages to look like they are supposed to. They work and in a very robust way (yeah, i would say that wouldnt i?) but they dont look impressive. The reason im confident about the system will be dealt with in the next post.
So, in summary, IMO, the Model View Presenter is great but hard to do right.