Home > Development > Web Forms MVP Presenter and Friends

Web Forms MVP Presenter and Friends


Hi all,
Its been a long time between posts. I have been flat out at work and way to busy. But I am currently building a MVP based Web Forms applications so I thought I would show the framework class involved and hopefully this helps you.

Here is the base view interface class.

    public interface IView
    {
        void DisplayMessage(string message);
        void DisplayError(string message);
        void Reload();
    }

I also have a couple of default base class which implement these methods as shown here.

    public static class FindControlUtility
    {
        public static Control FindControlRecursive(Control Root, string Id)
        {
            if (Root.ID == Id)
                return Root;

            foreach (Control Ctl in Root.Controls)
            {
                Control FoundCtl = FindControlRecursive(Ctl, Id);
                if (FoundCtl != null)
                    return FoundCtl;
            }

            return null;
        }
    }

    public abstract class WebFormsViewBase : Page, IView
    {

        public void DisplayMessage(string message)
        {

            Label messageLabel = FindControlUtility.FindControlRecursive(Master, "lblMessage") as Label;
           
            if(messageLabel == null)
                return;

            messageLabel.Visible = true;
            messageLabel.Font.Bold = true;
            messageLabel.ForeColor = Color.Green;
            messageLabel.Text = message;
        }

        public void DisplayError(string message)
        {
            Label messageLabel = FindControlUtility.FindControlRecursive(Master, "lblMessage") as Label; 

            if (messageLabel == null)
                return;

            messageLabel.Visible = true;
            messageLabel.Font.Bold = true;
            messageLabel.ForeColor = Color.Red;
            messageLabel.Text = message;

        }

        public void Reload()
        {
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    public class UserControlViewBase : UserControl, IView
    {

        public void DisplayMessage(string message)
        {
            Label messageLabel = Page.FindControl("lblMessage") as Label;

            if (messageLabel == null)
                return;

            messageLabel.Font.Bold = true;
            messageLabel.ForeColor = Color.Green;
            messageLabel.Text = message;
        }

        public void DisplayError(string message)
        {
            Label messageLabel = Page.FindControl("lblMessage") as Label;

            if (messageLabel == null)
                return;

            messageLabel.Font.Bold = true;
            messageLabel.ForeColor = Color.Red;
            messageLabel.Text = message;

        }

        public void Reload()
        {
            Response.Redirect(Request.Url.AbsolutePath);
        }

    }

Moving on I also use a presenter base class as shown below.

public abstract class PresenterBase<T>
        where T : IView
    {
        public abstract T View { get; }
        public abstract void InitializeView();
    }

For automatic dependency injection with out heap of service locator class heres the presenter factory.

    public static class PresenterFactory
    {
        public static Presenter BuildPresenter<Presenter, View>(View view)
            where View : IView
            where Presenter : PresenterBase<View>
        {
            IoC container = new IoC();
            var resolvedPresenter = container.Resolve<Presenter>(view);
            return resolvedPresenter;
        }
    }

So putting it together here is how I wire up a presenter in my web form in the code behind.

InvoicePresenter _presenter = PresenterFactory.BuildPresenter<InvoicePresenter, IInvoiceView>(this);

Any questions post a comment more source can be post to further explain and help.

Blair…

Keep it Agile.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: