Skip to content

How to create View and Presenter

marektihkan edited this page Sep 13, 2010 · 1 revision

Workflow

  1. Create Presenter interface to Presentation.Presenters.(BoundedContext) namespace
  2. Create Presenter implementation to Presentation.Presenters.(BoundedContext).Impl namespace and inherit from its interface
  3. Create View interface to Presentation.Views.(BoundedContext)
  4. Create Page/UserControl to Web.(BoundedContext) namespace and inherit from its interface and Arc.Infrastructure.Presentation.Mvp.Page/UserControl<TPresenterInterface>
  5. Add presentation logic to Presenter
  6. Override HookUpEvents on Page/UserControl
  7. Register events to presenter using Presenter property and Arc.Domain.Dsl if needed
  8. Register Presenter to Service Locator if needed (use conventions for less configuration)

Remarks

  • Presenter’s constructor should have parameter named “view” with type of View’s intetrface

Example


namespace ExampleSolution.Presentation.Presenters.Examples
{
    public interface IExamplePresenter
    {
        void Initialize();
        void ChangeGreeting();
    }
}

namespace ExampleSolution.Presentation.Presenters.Examples.Impl
{
    public class ExamplePresenter : IExamplePresenter
    {
        private readonly IExampleView _view;

        public ExamplePresenter(IExampleView view)
        {
            _view = view;
        }

        public void Initialize()
        {
            _view.Greeting = "Goodbye, World";
        }

        public void ChangeGreeting()
        {
            var greeting =  _view.NewGreeting;

            if (!string.IsNullOrEmpty(greeting))
            {
                _view.Greeting = greeting;
                _view.NewGreeting = string.Empty;
            }
        }
    }
}

namespace ExampleSolution.Presentation.Views.Examples
{
    public interface IExampleView
    {
        string Greeting { get; set; }
        string NewGreeting { get; set; }
    }
}

Default.aspx

<h1><asp:Label ID="greetingLabel" runat="server" /></h1>
<p>
  <asp:TextBox ID="newGreetingTextBox" runat="server" /> <asp:Button ID="submitButton" runat="server" Text="Tervita" />
</p>

Default.aspx.cs

namespace ExampleSolution.Web.Examples
{
    public partial class Default : Page<IExamplePresenter>, IExampleView
    {
        protected override void HookupEventHandlers()
        {
            Load += (x, y) => Presenter.Initialize();
            submitButton.Click += (x, y) => Presenter.ChangeGreeting();
        }

        public string Greeting
        {
            get { return greetingLabel.Text; }
            set { greetingLabel.Text = value; }
        }

        public string NewGreeting
        {
            get { return newGreetingTextBox.Text; }
            set { newGreetingTextBox.Text = value; }
        }
    }
}