The Start Menu……..Exposed What you never knew existed
Model View Presenter in ASP.NET 2.0
Introductions Nick Parker Tim Gifford
Gifford Consulting Embedded Agile Coaching –Adaptive, Informed Planning –Simple Design –Refactoring –Test-Driven Development –Continuous Integration –Self-Managing Teams –Approaching Deadline Behaviors
Nick Parker Two Rivers Marketing Microsoft Visual C# MVP
Agenda Compare Code-Behind vs. MVP Demo 1 MVP Definition Comparison to Model View Controller Demo 2 Advantages Agile Testing Overview Disadvantages Review/Resources/Q&A
Expectations?
Code-Behind vs MVP (Code) protected void btnAdd_Click(object sender, EventArgs e) { txtFirstName.Enabled = true; txtLastName.Enabled = true; txt Address.Enabled = true; btnSave.Enabled = true; btnSave.Visible = true; btnAdd.Enabled = false; btnAdd.Visible = false; btnCancel.Enabled = true; btnCancel.Visible = true; txtContactId.Value = string.Empty; btnUpdate.Enabled = false; btnUpdate.Visible = false; } private void EditContact(Guid guidContactId) { //Get the contact factory = config.BuildSessionFactory(); ContactDTO contact = null; ISession session = factory.OpenSession(); contact = (ContactDTO)session.CreateCriteria( typeof(ContactDTO)).Add(Expression.Eq("ContactId", guidContactId)).UniqueResult(); session.Close(); if (contact != null) { txtFirstName.Text = contact.FirstName; txtLastName.Text = contact.LastName; this.txt Address.Text = contact. Address; this.txtFirstName.Enabled = true; txtLastName.Enabled = true; txt Address.Enabled = true; btnSave.Enabled = false; btnSave.Visible = false; btnCancel.Enabled = true; btnCancel.Visible = true; btnAdd.Enabled = false; btnAdd.Visible = false; txtContactId.Value = guidContactId.ToString(); btnUpdate.Enabled = true; btnUpdate.Visible = true; } protected void btnSave_Click(object sender, EventArgs e) { presenter.SaveContact(); } protected void btnUpdate_Click(object sender, EventArgs e) { presenter.UpdateContact(); } protected void btnAdd_Click(object sender, EventArgs e) { presenter.NewContact(); } protected void btnCancel_Click(object sender, EventArgs e) { presenter.Cancel(); } public IList DataSource { set { this.gvContacts.DataSource = value; this.gvContacts.DataBind(); } public ITextBoxAdapter EditFirstName { get { return new TextboxAdapter(this.txtFirstName); } } public ITextBoxAdapter EditLastName { get { return new TextboxAdapter(this.txtLastName); } } public ITextBoxAdapter Edit { get { return new TextboxAdapter(this.txt Address); } }
Code-Behind vs MVP Data Access Business Logic Domain Model Presentation User Interface Database Data Access Business Logic Domain Model Code Behind User Interface Database
Demo Swap Demo
Model – View – Presenter Model Presenter Update View User Actions
Model Responsibilities: –Stores Data Collaborators: –None Anti-Collaborators: –Model does NOT talk to the view –* Model’s don’t talk to me either!
View Responsibilities: Display and User Interface Delegate User Actions to Presenter Collaborators: Presenter Anti-Collaborators: Services
Presenter Responsibilities: Map between the UI and the Data Handle User Events Collaborators: View Model Service Anti-Collaborators: None*
Split Pattern Passive View –All synchronization between the View & Model is handled by the presenter –Minimal behavior on the View Supervising Controller/Presenter –Allows for more advanced Data Binding –More behaviors handled by view
MVP vs MVC MVP is MVC done “right” Data Access Model Presenter View Data Access Model Controller View MVPMVC
MVP UML(ish) Diagram
Advantages Reduce duplicate code Reduce maintenance Separation of Concerns Testable/Test-Driven Reduce UI defects
Agile Testing UI Integration Unit Tests Selenium Mercury Automated QA FIT FitNesse Nunit
Testing Layers
Demo Disable/Hide Controls
Disadvantages Complexity Learning Curve When NOT to use MVP? Quick & Dirty Short Application Lifetime Support Staff Not supported in technology (VB 6.0)
Review Benefit –Use MVP to create a unit tested UI –Helps minimize (or eliminate) defects Costs –Complexity
Resources Tim Gifford Blog: blogs.giffordconsulting.com Nick Parker Blog: Code: