none
Best UI design pattern for C# winform project RRS feed

  • Question

  • tell me how can i use design pattern like MVVM, MVP or MVC for my winform project. thanks
    Saturday, February 22, 2020 7:26 PM

Answers

  • MVP the UI design pattern is more than implementing seperation of duty at the UI. It's about the overall seperation of concerns for the entire solution. I converted the Windows form tutorial over to Winform  Core 3.0 using 3 layers that implements SoC.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    The link works and  you have to wait and then start scrolling down to see the forms and code I converted over to C# Winform Core 3.0 with MVP and 3 layers

    http://www.vbforums.com/showthread.php?540421-Tutorial-An-Introduction-to-the-ADO-NET-Entity-Framework

    How I learned MVP many years ago that I have used in Web and Window form UI(s) projects professionally.

    http://polymorphicpodcast.com/shows/mv-patterns/

    The tutorial show the Presenters in its own classlib project, but you don't have to do it and just make folders called iView and Presenters in the Win form project and put the IViews and Presenters in their folders.

    using System;
    using System.Collections.Generic;
    using Entities;
    
    namespace PubCompanyWinCore.IView
    {
        public interface IAuthorView
        {
            int AuthorId { get; set; }
            string FirstName { get; set; }
            string LastName { get; set; }
            string Message { get; set; }
    
            event EventHandler<EventArgs> Send;
            event EventHandler<EventArgs> Delete;
            
            List<DtoAuthor> GetAuthors();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using BLL;
    using Entities;
    using PubCompanyWinCore.IView;
    using PubCompanyWinCore.Presenters;
    
    namespace PubCompanyWinCore
    {
        public partial class AuthorView : Form, IAuthorView
        {
            private IAuthorDM _authorDm;
            private AuthorViewPresenter mPresenter;
            private List<DtoAuthor> dtoauthors = new List<DtoAuthor>();
            private int currentauthoridx;
    
            public AuthorView(IAuthorDM authorDm)
            {
                _authorDm = authorDm;
                InitializeComponent();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                mPresenter = new AuthorViewPresenter(this, _authorDm);
                AuthorView_Load();
            }
    
            private void AuthorView_Load()
            {
                dtoauthors = GetAuthors();
                
                currentauthoridx = 0;
                PopulateFields(0);
            }
            private void tbxFirstName_TextChanged(object sender, EventArgs e)
            {
                dtoauthors[currentauthoridx].FirstName = tbxFirstName.Text;
            }
    
            private void tbxLastName_TextChanged(object sender, EventArgs e)
            {
                dtoauthors[currentauthoridx].LastName = tbxLastName.Text;
            }
    
            private void btnFirst_Click(object sender, EventArgs e)
            {
                PopulateFields(0);
            }
    
            private void btnPrev_Click(object sender, EventArgs e)
            {
                currentauthoridx = currentauthoridx - 1;
                PopulateFields(currentauthoridx);
            }
    
            private void btnNext_Click(object sender, EventArgs e)
            {
                currentauthoridx = currentauthoridx + 1;
                PopulateFields(currentauthoridx);
            }
    
            private void btnLast_Click(object sender, EventArgs e)
            {
                PopulateFields(dtoauthors.Count - 1);
            }
    
            private void btnClear_Click(object sender, EventArgs e)
            {
               
                AuthorId = 0;
                FirstName = "";
                LastName = "";
                lblAuthorID.Text = "000";
                tbxFirstName.Text = "";
                tbxLastName.Text = "";
                btnSend.Enabled = false;
                btnDelete.Enabled = false;
            }
    
            private void btnUpdate_Click(object sender, EventArgs e)
            {
                AuthorId = int.Parse(lblAuthorID.Text);
                FirstName =   dtoauthors[currentauthoridx].FirstName;
                LastName = dtoauthors[currentauthoridx].LastName;
                btnSend.Enabled = true;
                btnDelete.Enabled = true;
            }
    
            private void btnSend_Click(object sender, EventArgs e)
            {
                Send?.Invoke(this, EventArgs.Empty);
                MessageBox.Show(Message, "",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnSend.Enabled = false;
    
                if (Message.Contains("add"))
                {
                    dtoauthors = GetAuthors();
                    currentauthoridx = dtoauthors.Count - 1;
                    PopulateFields(currentauthoridx);
                }
            }
    
            private void btnNew_Click(object sender, EventArgs e)
            {
                AuthorId = 0;
                FirstName = "";
                LastName = "";
                lblAuthorID.Text = "000";
                tbxFirstName.Text = "";
                tbxLastName.Text = "";
    
                var dto = new DtoAuthor {AuthorId = 0, FirstName = "", LastName = ""};
                dtoauthors.Add(dto);
    
                currentauthoridx = dtoauthors.Count - 1;
    
                btnSend.Enabled = false;
                btnDelete.Enabled = false;
            }
            private void btnDelete_Click(object sender, EventArgs e)
            {
                DialogResult result1 = MessageBox.Show("Are you sure you want to delete the Author?",
                    "Important Question",
                    MessageBoxButtons.YesNo);
    
                if (result1 == DialogResult.No)
                    return;
    
                AuthorId = dtoauthors[currentauthoridx].AuthorId;
                Delete?.Invoke(this, EventArgs.Empty);
    
                MessageBox.Show(Message, "",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                AuthorView_Load();
                btnSend.Enabled = false;
    
            }
    
            private void PopulateFields(int idx)
            {
                if ((idx < 0 || idx > dtoauthors.Count - 1))
                {
                    if (idx < 0)
                    {
                        currentauthoridx = 0;
                        idx = 0;
                    }
    
                    if (idx > dtoauthors.Count - 1)
                    {
                        currentauthoridx = dtoauthors.Count - 1;
                        idx = dtoauthors.Count - 1;
                    }
                }
    
                var dto = dtoauthors[idx];
                
                FirstName = dto.FirstName;
                LastName = dto.LastName;
    
                lblAuthorID.Text = dto.AuthorId.ToString();
                tbxFirstName.Text = dto.FirstName;
                tbxLastName.Text = dto.LastName;
                btnSend.Enabled = false;
                btnDelete.Enabled = true;
            }
    
            public int AuthorId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Message { get; set; }
    
            public event EventHandler<EventArgs> Send;
            public event EventHandler<EventArgs> Delete;
    
            public List<DtoAuthor> GetAuthors()
            {
                return mPresenter.GetAuthors();
            }
        }
    }
    
    

    using System;
    using System.Collections.Generic;
    using System.Text;
    using BLL;
    using Entities;
    using PubCompanyWinCore.IView;
    
    namespace PubCompanyWinCore.Presenters
    {
        
        public class AuthorViewPresenter
        {
            private IAuthorView mView;
            private IAuthorDM _authorDm;
    
            public AuthorViewPresenter(IAuthorView authorView, IAuthorDM authorDm)
            {
                mView = authorView;
                _authorDm = authorDm;
                this.Initialize();
            }
    
            private void Initialize()
            {
                this.mView.Send += new EventHandler<EventArgs>(mView_Send);
                this.mView.Delete += new EventHandler<EventArgs>(mView_Delete);
            }
    
            private void mView_Delete(object sender, EventArgs e)
            {
                mView.Message = _authorDm.Delete(mView.AuthorId);
            }
    
            private void mView_Send(object sender, EventArgs e)
            {
                var dto = new DtoAuthor{AuthorId = mView.AuthorId, FirstName = mView.FirstName, LastName = mView.LastName};
    
                if (dto.AuthorId > 0)
                {
                    mView.Message = _authorDm.Update(dto);
                }
                else
                {
                    mView.Message = _authorDm.Add(dto);
                }
            }
    
            public List<DtoAuthor> GetAuthors()
            {
                return _authorDm.GetAuthors();
            }
        } 
    }
    


    • Marked as answer by Sudip_inn Sunday, February 23, 2020 7:50 AM
    Saturday, February 22, 2020 10:23 PM

All replies

  • MVP the UI design pattern is more than implementing seperation of duty at the UI. It's about the overall seperation of concerns for the entire solution. I converted the Windows form tutorial over to Winform  Core 3.0 using 3 layers that implements SoC.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    The link works and  you have to wait and then start scrolling down to see the forms and code I converted over to C# Winform Core 3.0 with MVP and 3 layers

    http://www.vbforums.com/showthread.php?540421-Tutorial-An-Introduction-to-the-ADO-NET-Entity-Framework

    How I learned MVP many years ago that I have used in Web and Window form UI(s) projects professionally.

    http://polymorphicpodcast.com/shows/mv-patterns/

    The tutorial show the Presenters in its own classlib project, but you don't have to do it and just make folders called iView and Presenters in the Win form project and put the IViews and Presenters in their folders.

    using System;
    using System.Collections.Generic;
    using Entities;
    
    namespace PubCompanyWinCore.IView
    {
        public interface IAuthorView
        {
            int AuthorId { get; set; }
            string FirstName { get; set; }
            string LastName { get; set; }
            string Message { get; set; }
    
            event EventHandler<EventArgs> Send;
            event EventHandler<EventArgs> Delete;
            
            List<DtoAuthor> GetAuthors();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using BLL;
    using Entities;
    using PubCompanyWinCore.IView;
    using PubCompanyWinCore.Presenters;
    
    namespace PubCompanyWinCore
    {
        public partial class AuthorView : Form, IAuthorView
        {
            private IAuthorDM _authorDm;
            private AuthorViewPresenter mPresenter;
            private List<DtoAuthor> dtoauthors = new List<DtoAuthor>();
            private int currentauthoridx;
    
            public AuthorView(IAuthorDM authorDm)
            {
                _authorDm = authorDm;
                InitializeComponent();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                mPresenter = new AuthorViewPresenter(this, _authorDm);
                AuthorView_Load();
            }
    
            private void AuthorView_Load()
            {
                dtoauthors = GetAuthors();
                
                currentauthoridx = 0;
                PopulateFields(0);
            }
            private void tbxFirstName_TextChanged(object sender, EventArgs e)
            {
                dtoauthors[currentauthoridx].FirstName = tbxFirstName.Text;
            }
    
            private void tbxLastName_TextChanged(object sender, EventArgs e)
            {
                dtoauthors[currentauthoridx].LastName = tbxLastName.Text;
            }
    
            private void btnFirst_Click(object sender, EventArgs e)
            {
                PopulateFields(0);
            }
    
            private void btnPrev_Click(object sender, EventArgs e)
            {
                currentauthoridx = currentauthoridx - 1;
                PopulateFields(currentauthoridx);
            }
    
            private void btnNext_Click(object sender, EventArgs e)
            {
                currentauthoridx = currentauthoridx + 1;
                PopulateFields(currentauthoridx);
            }
    
            private void btnLast_Click(object sender, EventArgs e)
            {
                PopulateFields(dtoauthors.Count - 1);
            }
    
            private void btnClear_Click(object sender, EventArgs e)
            {
               
                AuthorId = 0;
                FirstName = "";
                LastName = "";
                lblAuthorID.Text = "000";
                tbxFirstName.Text = "";
                tbxLastName.Text = "";
                btnSend.Enabled = false;
                btnDelete.Enabled = false;
            }
    
            private void btnUpdate_Click(object sender, EventArgs e)
            {
                AuthorId = int.Parse(lblAuthorID.Text);
                FirstName =   dtoauthors[currentauthoridx].FirstName;
                LastName = dtoauthors[currentauthoridx].LastName;
                btnSend.Enabled = true;
                btnDelete.Enabled = true;
            }
    
            private void btnSend_Click(object sender, EventArgs e)
            {
                Send?.Invoke(this, EventArgs.Empty);
                MessageBox.Show(Message, "",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnSend.Enabled = false;
    
                if (Message.Contains("add"))
                {
                    dtoauthors = GetAuthors();
                    currentauthoridx = dtoauthors.Count - 1;
                    PopulateFields(currentauthoridx);
                }
            }
    
            private void btnNew_Click(object sender, EventArgs e)
            {
                AuthorId = 0;
                FirstName = "";
                LastName = "";
                lblAuthorID.Text = "000";
                tbxFirstName.Text = "";
                tbxLastName.Text = "";
    
                var dto = new DtoAuthor {AuthorId = 0, FirstName = "", LastName = ""};
                dtoauthors.Add(dto);
    
                currentauthoridx = dtoauthors.Count - 1;
    
                btnSend.Enabled = false;
                btnDelete.Enabled = false;
            }
            private void btnDelete_Click(object sender, EventArgs e)
            {
                DialogResult result1 = MessageBox.Show("Are you sure you want to delete the Author?",
                    "Important Question",
                    MessageBoxButtons.YesNo);
    
                if (result1 == DialogResult.No)
                    return;
    
                AuthorId = dtoauthors[currentauthoridx].AuthorId;
                Delete?.Invoke(this, EventArgs.Empty);
    
                MessageBox.Show(Message, "",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                AuthorView_Load();
                btnSend.Enabled = false;
    
            }
    
            private void PopulateFields(int idx)
            {
                if ((idx < 0 || idx > dtoauthors.Count - 1))
                {
                    if (idx < 0)
                    {
                        currentauthoridx = 0;
                        idx = 0;
                    }
    
                    if (idx > dtoauthors.Count - 1)
                    {
                        currentauthoridx = dtoauthors.Count - 1;
                        idx = dtoauthors.Count - 1;
                    }
                }
    
                var dto = dtoauthors[idx];
                
                FirstName = dto.FirstName;
                LastName = dto.LastName;
    
                lblAuthorID.Text = dto.AuthorId.ToString();
                tbxFirstName.Text = dto.FirstName;
                tbxLastName.Text = dto.LastName;
                btnSend.Enabled = false;
                btnDelete.Enabled = true;
            }
    
            public int AuthorId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Message { get; set; }
    
            public event EventHandler<EventArgs> Send;
            public event EventHandler<EventArgs> Delete;
    
            public List<DtoAuthor> GetAuthors()
            {
                return mPresenter.GetAuthors();
            }
        }
    }
    
    

    using System;
    using System.Collections.Generic;
    using System.Text;
    using BLL;
    using Entities;
    using PubCompanyWinCore.IView;
    
    namespace PubCompanyWinCore.Presenters
    {
        
        public class AuthorViewPresenter
        {
            private IAuthorView mView;
            private IAuthorDM _authorDm;
    
            public AuthorViewPresenter(IAuthorView authorView, IAuthorDM authorDm)
            {
                mView = authorView;
                _authorDm = authorDm;
                this.Initialize();
            }
    
            private void Initialize()
            {
                this.mView.Send += new EventHandler<EventArgs>(mView_Send);
                this.mView.Delete += new EventHandler<EventArgs>(mView_Delete);
            }
    
            private void mView_Delete(object sender, EventArgs e)
            {
                mView.Message = _authorDm.Delete(mView.AuthorId);
            }
    
            private void mView_Send(object sender, EventArgs e)
            {
                var dto = new DtoAuthor{AuthorId = mView.AuthorId, FirstName = mView.FirstName, LastName = mView.LastName};
    
                if (dto.AuthorId > 0)
                {
                    mView.Message = _authorDm.Update(dto);
                }
                else
                {
                    mView.Message = _authorDm.Add(dto);
                }
            }
    
            public List<DtoAuthor> GetAuthors()
            {
                return _authorDm.GetAuthors();
            }
        } 
    }
    


    • Marked as answer by Sudip_inn Sunday, February 23, 2020 7:50 AM
    Saturday, February 22, 2020 10:23 PM
  • Can you please share a small sample project code of winform project where MVP has been implemented. thanks
    Monday, February 24, 2020 3:09 PM
  • Can you please share a small sample project code of winform project where MVP has been implemented. thanks

    I believe I did that with the code that has been presented. It's best you view the shows particularly the one About Windows form project and MVP along with the other shows.

    http://polymorphicpodcast.com/shows/mv-patterns/

     
    Monday, February 24, 2020 6:03 PM