Is ASP.net Model View Presenter worth the time? - asp.net

I'm reading about ASP.net MVP pattern over this weekend and it seem like even the most simple task take too much effort if do it in MVP pattern the pay off seem to be at larger project but i think to myself if i'm going to follow MVP. Why not just do the project in ASP.net MVC?
The reason that I'm looking at MVP pattern is because I've noticed in all my ASP.net Webform projects there are a lot of code in the code behind just for event handling along if i have a lot of server control on the web form so i was looking at the way to reduce that and come across the MVP pattern.
Is it worth the effort to follow the MVP pattern or just switch over to the ASP.net MVC?

I would recommend reading the following two links to get you up to speed on MVP and MVC :
A great blog post by Gil Fink on the different advantages of MVP and MVC
A StackOverFlow question on whats the difference between MVP and MVC
Should you switch over?
Based on what you've told me I would recommend that you use the Passive MVP model mentioned in the article above.
My main assumptions are:
Your dealing with an existing codebase of WebForms apps
You need to use ThirdParty .Net controls for existing functionality
Your working on existing apps and don't have time to re architect them
Any ASP.Net Web Apps you work on in the future, you can incrementally apply the passive MVP and get the benefits of TDD straight away
Your View (codebehind + aspx) essentially become dumb and just perform simple tasks:
take information given by the presenter
responds to events and provides information back to the presenter
I've used this model extensively for Web Forms development and I couldn't imagine not being able to Unit Test my Model and Presenter code. Once you establish your base model which doesn't take very long and seen the power of Unit Testing, working with Web Forms becomes enjoyable.
Some links to MVP stuff that the model I've used is based on:
Easy Example of MVP model
Simple MVP model for ASP.Net
An MVP and MVC example for ASP.Net
I would also recommend that you learn MVC to.
When time permits, take an existing App and port it to MVC. This way your sole focus is getting to know MVC and when you move logic into the MVC pattern, you'll discover things that you implemented in WebForms and never gave much thought to but now need to solved in another way. Great way to compare the patterns and see what works for you.
Hope this helps, feel free to ask any questions.

If you start a new project, then ASP.net MVC is a better choice. But if you just want to refactor an existing project as you just said, then MVP is the option because there is no easy way to convert those web-forms codes to MVC .

In my opinion, the ideal way to go for new applications is MVC. However, if you have a lot of code already using WebForms, then the MVP pattern is the way to go.

I would go Asp.Net MVC if it's a new project, but I agree, MVP might be a good pattern for legacy webforms projects.
Here's an example from my blog:
http://www.unit-testing.net/CurrentArticle/How-To-Use-Model-View-Presenter-With-AspNet-WebForms.html

my personal opinion is that if there's a lot of code in the code behind, there still are ways other than adopting M-V-P in order to slim it down, refactor and make it testable.
if your page has extensive user interaction (like buttons/links enabling/disabling, panels and controls appearing/disappearing) M-V-P would be worth the hassle.

Related

Can MVC do something Web Forms can't? (asp.net)

I'm sort of new to asp.net. I know both MVC and Web Forms have their advantages and disadvantages, but I'm more familiar with Web Forms so I've only used that.
I was just wondering if there's something MVC can do that Web Forms can't?
From what I've understood, it's basically just different ways to code but you can get the same results from either.
This may have already been answered but I can't seem to find something that's only doable by MVC or only by Web Forms.
Thanks.
Asking "Can MVC do something that Webforms can't" is like asking "can you store papers in a well organized filing cabinet that you couldn't in a pile on your desk?" The answer, really, is no. The difference between MVC and Webforms is not functionality, it's ease of development, ease of maintainance and scalability. There are problems that webforms handles more naturally (very few IMHO, but they exist) and problems that MVC handles more naturally.
As an additional point, your question could actually be applied to any two frameworks or languages (assuming that some component of the framework is Turing Complete). Not only could you do anything in Webforms in MVC and visa versa, but you could implement the entire functionality of either in C, or R, or COBOL, or TSQL. Choosing a programming tool is about what it can do but what it does easily.
Web forms do everything which can MVC. Its just matter of maintainability, testing, separation between layers etc.
See Java can do everything which can C# , but C# have some more features then Java which make programmer's life easy .
Mostly programmer use new Design Patterns due to their advance features like AngularJs now replacing Jquery due to its advance features like Declarative Way, Directive, Routes, Two way binding. Jquery can also do everything which AngularJs can. But AngularJs more robust then jquery in terms of performance, testing, maintainability, separation of concerns.

Should I dive into ASP.NET MVC or start with ASP.NET Webforms?

I plan to pick up Silverlight in the future.
Possibility of going into Microsoft WPF.
Currently learning Objective-C 2.0 w/ Cocoa.
I already know Pros and Cons of ASP.NET MVC vs ASP.NET Webforms. What I want to know is what would be more "efficient" for me to learn given the circumstances above?
By efficient I mean learning one design pattern once and then re-using it. Objective-C I believe uses MVC approach? What about Silverlight? WPF?
I'll be going to B&N tomorrow to pick up an ASP.NET book so I need to decide right now between webforms and mvc.
Also as a side question is it true that ASP.NET Webforms is often used by freelancers/small companies and ASP.NET MVC in large enterprises?
I truly believe that ASP.NET MVC is more aligned to how the web works as winforms, but that doesn't mean anyone should just ditch ASP.NET Webforms and just use ASP.NET MVC. I think you should attempt to look at both, regardless of what your future plans are.
As far as I can tell, the pattern used commonly amongst WPFers is MVVM (Model, View, View-Model).
As for your last question, regarding the use of ASP.NET MVC in large enterprises vs ASP.NET Webforms in small companies. I believe that you should pick the technology (talking specifically about mvc vs webforms) that suites your coding style the best.
There are advantages and disadvantages to both.
I second PieterG's comment and would like to add:
If you are looking to learn a platform on which you want to put different kinds of view layers on top, I would highly recommend MVC over Web forms. Another way to ask the question would be: in your application, does data have first class status?
I build mostly reporting applications, so the answer for me is: yes!
Based on personal experience, its a lot easier for me to build a set of ReSTful controllers which handle business logic. Then when I want to push it to a RIA front-end (I do Flex), all I do is add another ReSTFul method to get me the data in XML so I can work with it in Flex. So, for example if I have Urls like this that return HTML (i.e. aspx) pages:
example.com/stuff/
I can then add a method (or even just a route in the route table) to do this:
example.com/stuff/xml
In other words, I am able to turn my controller into a data service with minimal effort and this has worked very well for me for exporting to XML, Json, even Excel. Doing the same in web forms is very painful.

ASP .NET MVC 2.0 or WebFoms to develop an auction site like Ebay?

I´d like to know what is the best option to develop an auction site like Ebay with ASP .NET 4.0:
MVC 2.0
WebForms
I only worked with WebForms, but I have time to learn MVC if is the best option...
What you think?
MVC, but if you choose to use MVC do take the opportunity to write unit tests for your components. May I suggest a Test Drive Design approach?
While you are at it, you might also want to look in to Behavior Driven Design (it is like TDD, but few levels more abstracted)
This article http://www.lostechies.com/blogs/chad_myers/archive/2009/04/27/to-mvc-or-to-webforms.aspx has good arguments why you would want to use MVC over WebForms. You can read through the 4 bullet points and if they don't bother you then WebForms will be fine.
In a nutshell.
WebForms are fast to develop.
WebControls add ViewState and have
PostBacks.
If you come from another language
WebForms might not be OO enough for
you.
WebForms are difficult to Unit Test
If you anticipate a high volume site then not having ViewState and being able to unit test should be important to you.
I would recommend MVC, because I would recommend MVC for anything! It is easy to pick up, and you will probably never go back to webforms.
Besides the simplicity and the clean mark up. If you are working with any sort of generated code, the model binding makes developing pages really fast and easy. I have used a custom generated BLL and DLL, when the DataAnnotations attributes are generated all client side and server side validation is automatic.

Prerequisite knowledge for ASP.NET MVC

After being frustrated in my attempts to learn the arcana that is ASP.NET, I decided to try ASP.NET MVC.
I have extensive experience with C# 2.0 doing just about everything (WinForms, Windows Services, remoting, ...) except ASP.NET . I've been writing PHP for even longer than I've been coding .NET. With all of this experience, I thought I was ready for ASP.NET MVC. I now see I'm learning the following simultaneously and having a bit of a hard time with it:
the MVC pattern in general
ASP.NET
ASP.NET MVC
.NET 3.5.
I'm currently about 1/2 way through the NerdDinner free book chapter. I don't feel completely lost but I do get the feeling that I'm not catching on to as much as I'd like to (due to the fact that I'm learning 4 things at once -- sometimes it's not clear which skills are from which abstraction layer).
What prerequisite knowledge would you recommend for ASP.NET MVC? Any/all of those I listed above? Others? Or maybe I'm just slow and most people would be fine doing what I did.
I have been an ASP.Net developer for just 3 years now. I got a project from a friend and decided to develop it using ASP.Net MVC to learn this new stuff.
I must admit I felt lost in the beginning as well, but then I saw some very interesting articles on the ASP.Net/MVC site. At the bottom are 7 iterations which will give you a very detailed of what you are doing while you are doing it.
I used the Products example they have there and applied sortof the same to my own project.
After all the iterations, I now feel like I have a very good base to start learning the more advanced MVC implementations. (JQuery plugins/Ajax/...)
http://www.asp.net/learn/mvc/
I think some basic understanding of ASP.Net is a base requirement. If you have that, you should be able to get through this with just a little bit of motivation.
"MVC pattern in general" - no need but useful.
ASP.NET - also not necessary.
.NET 3.5 - yes. It will be your working tool, you should master it.
Very useful would be the understanding of multi-tier architecture since this time you will have to think first about how you organize your application, opposite to direct hacking with controls in WebForms.
Also you will not avoid knowledge of HTML/CSS, maybe JavaScript.
Just don't be scared. Do it one step at a time and you will get it. Just don't read too much. Practice it.
Lots of highly useful tutorials are to be found here: ASP.NET MVC Tutorials
Also read blogs from ASP.NET MVC "parents":
http://haacked.com
http://weblogs.asp.net/scottgu/default.aspx
http://www.hanselman.com/
http://stephenwalther.com/blog/Default.aspx
I have a similar background. I have used .Net/C# since its inception, but all system related, no web experience. About 8 months ago, I decided that I wanted to learn about the web and decided that for me ASP.NET MVC seemed like the right way to go. I don't feel that my lack of ASP.NET experience was too much of a hindrance. I used the http://www.asp.net/mvc/ site as the center for my learning. All the videos are good, plus the tutorials and all the sample projects. I have downloaded most of them and have enough familiarity that when I can't figure something out I go to one of the downloaded projects with a similar requirement and look to see how it was coded. Knowing no web technology, I also ordered books on HTML, CSS, JAVA script, AJAX and Jquery. For me the best way to learn is to develop a project, so that it what I did. Most of the authors of the new MVC books also have blogs with lots of good information. When I started to learn about MVC, the MVC books hadn't been published yet, but I am sure there are some good ones out there by now or that will be available soon.
I have enjoyed the new experience and have learned a lot, I think the new technology is great. Good Luck!
I recommend getting a book on ASP.NET MVC and working through the examples in the book. Don't worry too much about the history of the MVC design pattern or it's 1979 roots at Xerox PARC. I wouldn't bother reading much more about the actual MVC pattern than this simple article from MSDN: Cutting Edge: ASP.NET Presentation Patterns
I recently finished Steve Sanderson's Pro ASP.NET MVC Framework and I can recommend it. It has a multiple chapter sample application that gets you started quickly with ASP.NET MVC and then 10 or so follow-up chapters that cover the relevant topics in depth (Controllers, Views, Security, etc). The book has a strong focus on unit testing and dependency injection and also covers basic object relational mapping with LINQ to SQL. There is also chapters that cover integrating jQuery with ASP.NET MVC and how you might utilize framework components (authorization, membership, roles, personalization, caching, etc) from traditional WebForms applications.
There are a number of positive reviews on Steve's Blog which is also a good source of additional ASP.NET information. Amazon.com has a couple of positive reviews on the book (I need to add mine when I get some free time). You can also preview some of the book over at Google Books.
Some of the other books that are available now or available shortly are:
Wrox: Beginning ASP.NET MVC 1.0 --- Both authors
Keyvan Nayyeri and Simone Chiaretta are active ASP.NET MVC bloggers. There is a sample chapter on testing for download here. I've read that it should be available at the end of June 2009?
Manning: ASP.NET MVC in Action --- This book is by Ben Scheirman, Jeffrey Palermo and Jimmy Bogard. They all have interesting blogs that cover ASP.NET MVC related topics. If I heard correctly, this book should be out in August 2009. You can pre-order the book (MEAP = Manning Early Access Program) here and get access to the first 11 chapters (unedited or loosely edited I think). The CodeCampServer reference application that complements the book is pretty intense and covers using nHibernate (ORM), Castle Windsor (DI / IoC), DDD, unit, integration and regression testing.
Wrox: Professional ASP.NET MVC 1.0 --- This is the book that accompanies the NerdDinner sample application and reader that was made available a while back. It's authored by some pretty smart Microsoft guys: Rob Conery, Scott Guthrie, Scott Hanselmann and Phil Haaaaaaaaaaaaack who have the inside information on why certain decisions were made with the framework. Available now.
Packt: ASP.NET MVC 1.0 Quickly --- The author Maarten Balliauw has a pretty good blog that talks about various ASP.NET MVC issues including testing. Available now.
Sams: ASP.NET MVC Framework Unleashed --- Author Stephen Walther is responsible for a number of popular ASP.NET Unleashed books from the early 1.0 days of ASP.NET and now he is writing a book on ASP.NET MVC. You can check out his blog for sample content from the upcoming book and other ASP.NET MVC related posts. Amazon says this book will be available in July 2009.
Wrox: ASP.NET MVC Website Programming Problem Design Solution --- This appears to be a follow-up to the book Wrox: ASP.NET 2.0 Website Programming: Problem Design Solution and welcomes back author Marco Bellinaso and introduces two new authors to the project Nick Berardi and Al Katawazi. The sample application for the book is the Beerhouse CMS and is available for download on CodePlex. The book's website says that it will be released in June 2009.
I think once one wraps one's head around the idea that MVC uses routing to direct requests straight to classes (which use a model for connections and a view for display) rather than showing "actual" pages, the concept of MVC in a web application really presents itself more clearly. With the 'difficulty of concept' out of the way, it's a lot easier to consider those attributes and make a decision whether MVC is suited to a particular application.
edit -- having said that, if I were new to ASP.NET, I would probably tackle the forms version first and get a good understanding of events, postback, and the methodology of that system before going into MVC because ASP.NET MVC is really handled on top of that system.
edit 2 - I think I got voted down before finishing my edits :-(
This is a good introduction from Martin Fowler. He talks in detail about GUI Architectures.
Start reading some material on the following topics and they will really help you gear yourself towards any MVC in general. Here are some topics that you can look up online:
Routing.
The MVC Pattern.
Controllers and Actions.
Mapping parameters.
ActionResults.
Error handling.
Action filtering.
Passing data to a view.
HTML Helpers.
View rendering extensibility.
Using jQuery with ASP.NET MVC.
Best practice tips for views, controllers, and models.
MVC Anti-patterns.
If you don't already know Webforms, then stick to learning just MVC right now so that you don't confuse yourself. Webforms is a useful thing to know, and since you know Winforms already, it shouldn't be too hard to learn afterward.
MVC and Webforms both work under ASP.NET, so a lot of things you'll learn while playing with MVC will carry over to Webforms.
It might help to approach Asp.Net MVC from a different angle: Read the Agile Web Development With Rails book, follow along (implement using Rails), and try to implement a project of similar scope to what's in that book in ASP.Net MVC.
You'll pick up Rails quickly (a week or two of hacking around a couple of hours a night), and the explication of the conventions of Rails will guide you into a mindset that aligns fairly closely with the Asp.Net MVC way. It'll probably help you understand the motivations for MVC along the way, and you'll certainly have opinions about how things "should" work in Asp.net MVC.
I would spend some time on ASP.NET first before learning ASP.NET MVC. Remember, ASP.NET MVC is based on ASP.NET. Some things from traditional ASP.NET won't be used with the MVC model (user controls, (ASCX and ASPX files)) but it is a good thing to still understand these. If you want to focus on MVC, first learn ASP.NET but do not spend time studying the common controls or databinding.
The rest of ASP.NET is still useful (HttpModules, HttpHandlers, the web.config file, the membership provider, etc) when you're doing ASP.NET MVC, and I'd say likely a prerequisite to really using ASP.NET MVC well.
That said, there is a lot more work for ASP.NET out there than ASP.NET MVC. Some employers are looking for people who know MVC, but they are mainly doing that as they are looking for people who are passionate about learning new things. My concern with ASP.NET MVC is that I don't see how to integrate it with traditional ASP.NET applications. The reality is most jobs out there involve working with an existing codebase, not starting from scratch.
I'd been following ASP.Net MVC pretty much from the beginning, and I have to say, I love it. Combined with Unity (or another DI framework) it just flows nicely. I've always liked a certain separation of concerns principle anyhow, and favored ASP.Net over PHP (and classic ASP). I didn't always like the control pattern of ASP.Net though. MVC fits much better. And with Json() and PartialView() for return types interacting with jQuery.getJSON() and $("#mydiv").load() is very simplified on the server-side. It also works well when following SOLID Design principles...
It does help to have a solid understanding of HTML and JavaScript (AJAX, DHTML) for more interactive points. It also helps to think in terms of the MVC pattern, or not thinking about cluttering your controller and your data.
One thing I don't care as much for, is the lack of a built in skinning system, for multiple skins on a web-app. I've come around this, and having the source really helps. I would say as other have suggested, follow the blogs (and read back) of
Phil Haack,
Scott Guthrie, and
Scott Hanselman. They all have vital information. Beyond this, as also mentioned, the demos on the ASP.Net MVC Website are very good.
Apart from what others have recomended I would suggest the following two books:
Spring in Action : It's not ASP.NET MVC, it's about Spring, but the foundations are similar and it's really well written.
Pro ASP.NET MVC 2 Framework : It explains the concepts in an easy to follow way, and it helps introduce DI into ASP.NET MVC.
All previous post are so cool :) but i want to add my own view. Without some base of asp.net runtime mechanism you can't write a good app. This info you could get from dino esposito book(he has two books about asp.net 2.0 - basic and advanced level) in basic level - there is one chapter about processing incoming request - you could read only this chapter.
If you know what is httpapplication, httpruntime, request, response etc. just skip my answer.
I know from my brief time with a Ruby on Rails job, coming back to ASP.NET and working with MVC it was easy to apply what I learned from RoR to ASP.NET MVC. Truthfully, ASP.NET MVC is actually just about the VC part and not much about the Model since ASP.NET doesn't have to be very REST based whereas RoR practically enforces it.
The problem I find is trying to get people to learn correct conventions of MVC when they are first learning it by coming from ASP.NET itself. Like others have mentioned, it might be a good idea to learn a true MVC platform (in this case RoR) because trying to apply what you know about ASP.NET to a MVC architecture. A good place to start with RoR is http://railsforzombies.org/ It's also kind of fun and will give you the basics for what really goes where in the ideal REST MVC scenario. After that apply what you know to the MVC for ASP.NET.
The very first and basic thing with ASP.Net MVC is the Routing concepts. LEarn this well first. You can also go throught System.Web.Routing Namespace on getting some more idea on this. Routing is just telling the IIS that is there is a request for a particular extension then redirect to this handler.
The next thing would "Convention over configuration". For MVC you need to have a particular folder structure like Controller,Model and Views .Nothing more mystery here.

Tips from ASP.NET MVC and lessons for ASP.NET WebForms developers

What are some helpful things that ASP.NET MVC developers could suggest that would help us ASP.NET WebForms developers to write better code/web apps?
I'm a WebForms guy but with all the new hype around MVC I'd value some comments on helpful tips, tricks and strategies that might be able to be used in a webforms app.
Something that occurred to me a while ago when learning about the new MVC framework, is that WebForms was, I think, an attempt at MVC in many ways. The markup and code-behind comprise View and Controller, and you're left to write your own Model.
This idea goes hand in hand with the important design considerations I gained from learning about MVC. The most important of which is solidifying the core domain of your system as a whole and making sure all common logic is defined at a level that is reusable within this domain. This is your Model, and I like to call the logic that lives at this level Domain Logic (I mix terms, I know). Your Model should be reusable across different applications (a main web/winforms app, winforms apps for utility and configuration, background processing services, web services, etc.). Your applications should stay very specific to their purpose: they consist of Presentation Logic (their views) and Application Logic (their controllers). Anything that crosses the line of needing to be used in other applications is easily classifiable as Domain Logic, and should not be part of the application code for any given application.
I hope that makes sense.
The gist of it is, even if you're not using a pure MVC framework or object model or whatever, this high level look at design can be applied with great effect. Isolate common logic in a domain layer that is reusable across applications and your applications are much easier to write and extend and maintain.
Forget about page life cycle
There is no ViewState (by default that is)
There are no postbacks
You need to know the HTTP protocol basics (GET/POST). The same goes with HTML (DropDownList is actualy a tag)
I would recommend geting started by watching the screencasts on Microsoft's Learn site:
ASP.NET MVC Videos
There you will learn the differences between the WebForms postback model, and the way that MVC directly routes your URLs to Actions & Methods.
The second major difference is to remember that in ASP.NET MVC you do not use controls that do postbacks. It might feel a bit more like Classic ASP where code is inline on the page within server side blocks, but it is worth it. You will have full control over the HTML that is generated, and this is very good for things like Search Engine Optimization.
Some advices
Don't use standard controls. Instead pay attention jQuery, MooTools.
Use strongly typed Models
It's good practice to use some IoC like Spring.NET
I'm also coming from webforms, but have been learning asp.net MVC since around preview 3.
I'm not exactly sure how to bring anything from MVC into webforms, they are two entirely different frameworks. I'm probably too new with it to understand how any of it could be applied to webforms, but right now it seems they have very little in common.
Some of the strengths of MVC are it's rigid structure on how you need to do things and where you put code. It also does away with the form runat=server and I believe does not promote the usage of any of the asp.net server controls (you need to write all the HTML yourself). That is pretty much the definition of webforms right there. MVC pretty much replaces webforms with it's own framework.
If you ever did Classic ASP or PHP, I would call ASP.net MVC a more structured framework for what people might do with those older scripting languages. It's kind of like the FuseBox framework, but even more structured and evolved.

Resources