using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SharpSoutions.MVP
{
public interface IView
{
Model DataSource { get; set; }
void DataBind();
void UpdateModel();
event EventHandler Saved;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpSoutions.MVP.Domain;
namespace SharpSoutions.MVP
{
[Serializable]
public class Model
{
public Person NewPerson { get; set; }
public Title[] Titles { get; set; }
}
}
The presenter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpSoutions.MVP.Domain;
using System.Diagnostics;
namespace SharpSoutions.MVP
{
public class Presenter
{
private Data.IRepository<Title> titleRepository;
private Data.IRepository<Person> personRepository;
public Presenter(Data.IRepository<Title> titleRepository, Data.IRepository<Person> personRepository)
{
Trace.TraceInformation("Begin Presenter.Ctor");
this.titleRepository = titleRepository;
this.personRepository = personRepository;
this.Model = new Model();
this.Model.NewPerson = new Person();
Trace.TraceInformation("End Presenter.Ctor");
}
public Model Model { get; set; }
public IView View { get; set; }
/// <summary>
/// Init populates the "static" data
/// These are the data that is shown in dropdown boxes etc
/// </summary>
/// <param name="mockView"></param>
public void Init(IView view)
{
Trace.TraceInformation("Begin Presenter.Init");
AttachView(view);
this.View.DataSource = this.Model;
this.Model.Titles = this.titleRepository.Query();
this.View.DataBind();
Trace.TraceInformation("End Presenter.Init");
}
public void AttachView(IView view) {
this.View = view;
this.View.Saved += new EventHandler(View_Saved);
}
void View_Saved(object sender, EventArgs e)
{
System.Diagnostics.Trace.TraceInformation("Begin Presenter.View_Saved");
this.Save();
System.Diagnostics.Trace.TraceInformation("End Presenter.View_Saved");
}
public void Save()
{
System.Diagnostics.Trace.TraceInformation("Begin Presenter.Save");
this.View.UpdateModel();
personRepository.Update(this.Model.NewPerson);
System.Diagnostics.Trace.TraceInformation("End Presenter.Save");
}
}
}
The View implementation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharpSoutions.MVP.Domain;
namespace SharpSoutions.MVP.Web
{
public partial class View : System.Web.UI.UserControl, IView
{
public Model DataSource{
get {
if (ViewState["d"] != null) {
return (Model)ViewState["d"];
}
return null;
}
set {
ViewState["d"] = value;
}
}
public override void DataBind()
{
System.Diagnostics.Trace.TraceInformation("Begin View.Databind");
base.DataBind();
TitleDropDown.DataSource = this.DataSource.Titles;
if (this.DataSource.NewPerson.Title != null) {
TitleDropDown.SelectedValue = this.DataSource.NewPerson.Title.Id.ToString();
}
TitleDropDown.DataTextField = "Name";
TitleDropDown.DataValueField = "Id";
TitleDropDown.DataBind();
nameTextbox.Text = this.DataSource.NewPerson.Name;
System.Diagnostics.Trace.TraceInformation("End View.Databind");
}
public void UpdateModel()
{
System.Diagnostics.Trace.TraceInformation("Begin View.UpdateModel");
this.DataSource.NewPerson.Name = nameTextbox.Text;
ListItem selectedTitle = TitleDropDown.SelectedItem;
this.DataSource.NewPerson.Title = new Title { Id = int.Parse(selectedTitle.Value), Name = selectedTitle.Text };
System.Diagnostics.Trace.TraceInformation("End View.UpdateModel");
}
public event EventHandler Committed;
protected void SaveButton_Click(object sender, EventArgs e)
{
System.Diagnostics.Trace.TraceInformation("Begin View.SaveButton_Click");
if (this.Saved != null) {
this.Saved(this, EventArgs.Empty);
}
System.Diagnostics.Trace.TraceInformation("End View.SaveButton_Click");
}
public event EventHandler Saved;
}
}
No comments:
Post a Comment