MVP Sample for Winform

MVP Sample for Winform

์ด์ฒ ์šฐ

MVC ์—์„œ ๊ฐœ์„ ํ•œ ๊ฒƒ.

  • C# Winform์˜ DataBinding ๊ณผ INotifyPropertyChanged ๋ฅผ ์ด์šฉํ•˜์—ฌ
  • Model ๊ณผ View ์—์„œ
  • DataChangedEvent ๋ฅผ ๋บ๋‹ค.
  1. ๋จผ์ € Winform ํ”„๋กœ์ ํŠธ(.Net 7)๋ฅผ ๋งŒ๋“ ๋‹ค.
    0-1. ํ”„๋กœ์ ํŠธ ์†์„ฑ์—์„œ 'Console Applicationโ€™์„ ์„ ํƒํ•œ๋‹ค.
    0-2. Form1 ๋””์ž์ธ์—์„œ TextBoxData, ButtonReset, ButtonIncrease ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

  2. IView.cs

    public interface IView
    {
        TextBox TextBoxData { get; }

        event EventHandler<EventArgs>? ResetPressed;
        event EventHandler<EventArgs>? IncreasePressed;
    }
  1. Model.cs
    public class Model : INotifyPropertyChanged
    {
        public Model() { }
        public int Data
        {
            get => _data;
            set
            {
                if (_data != value)
                {
                    _data = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Data)));
                    Console.WriteLine(ToString());
                }
            }
        }
        private int _data = 0;

        public void Reset() => Data = 0;
        public void Increase() => Data++;
        public void Set(int data) => Data = data;

        public override string ToString() => $"Data:{Data}";

        public event PropertyChangedEventHandler? PropertyChanged;
    }

  1. Presenter.cs
    public class Presenter
    {
        public Model Model { get; private set; }
        public IView View { get; private set; }

        public Presenter(Model model, IView view)
        {
            Model = model;
            View = view;

            View.ResetPressed += View_ResetPressed;
            View.IncreasePressed += View_IncreasePressed;

            var binding = new BindingSource
            {
                DataSource = Model
            };
            View.TextBoxData.DataBindings.Add(new Binding("Text", binding, "Data", true, DataSourceUpdateMode.OnPropertyChanged));
        }

        private void View_ResetPressed(object? sender, EventArgs e)
        {
            Model.Reset();
        }
        private void View_IncreasePressed(object? sender, EventArgs e)
        {
            Model.Increase();
        }
    }
  1. IView ์ƒ์†๋ฐ›์€ Form1.cs
    public partial class Form1 : Form, IView
    {
        public Form1()
        {
            InitializeComponent();
        }

        TextBox IView.TextBoxData => TextBoxData;

        public event EventHandler<EventArgs>? ResetPressed;
        public event EventHandler<EventArgs>? IncreasePressed;

        private void ButtonReset_Click(object sender, EventArgs e)
        {
            ResetPressed?.Invoke(this, new EventArgs());
        }

        private void ButtonIncrease_Click(object sender, EventArgs e)
        {
            IncreasePressed?.Invoke(this, new EventArgs());
        }
    }

  1. Program.cs
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();

            #region MVP Sample for Winform.
            var view = new Form1();
            var model = new Model();
            _ = new Presenter(model, view);
            #endregion

            Application.Run(view);
        }
    }
2๊ฐœ์˜ ์ข‹์•„์š”