Upload
marjorie-king
View
219
Download
1
Embed Size (px)
Citation preview
The Start Menu……..ExposedWhat 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; txtEmailAddress.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.txtEmailAddress.Text = contact.EmailAddress; this.txtFirstName.Enabled = true; txtLastName.Enabled = true; txtEmailAddress.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<ContactDTO> 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 EditEmail { get { return new TextboxAdapter(this.txtEmailAddress); } }
Code-Behind vs MVP
Data Access
Business LogicDomain Model
Presentation
User Interface
Database
Data Access
Business LogicDomain Model
Code Behind
User Interface
Database
DemoSwap 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!
ViewResponsibilities:
Display and User Interface
Delegate User Actions to Presenter
Collaborators:
Presenter
Anti-Collaborators:Services
PresenterResponsibilities:
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
MVP MVC
MVP UML(ish) Diagram
Advantages• Reduce duplicate code
• Reduce maintenance
• Separation of Concerns
• Testable/Test-Driven
• Reduce UI defects
Agile Testing
Unit Tests
Integration
UISelenium
Mercury Automated QA
FIT FitNesse
Nunit
Testing Layers
Unit Int UI
Brittleness Execution Time
DemoDisable/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)
ReviewBenefit
– Use MVP to create a unit tested UI– Helps minimize (or eliminate) defects
Costs– Complexity
Resources
Tim GiffordEmail: [email protected]
Blog: blogs.giffordconsulting.com
Nick ParkerEmail: [email protected]
Blog: http://developernotes.com
Code:http://code.google.com/p/developernotes/