無聊學習一下MVP這個概念

記錄一下學習MVP,好處是便於替換前臺頁面(winfrom替換成asp.net),不改變頁面邏輯層及其之後的層數據庫

M:業務邏輯asp.net

V:頁面學習

P:頁面邏輯 ,充當 頁面和業務邏輯的中間層this

規則:V和M不能直接接觸,由P來作中間人spa

調用流程:V 去調用P,P去調用M,P調用M完成後,P調用V改變頁面(這裏爲了保證P調用V不出錯,使用了接口,規定V必須實現的方法).net

 

創建四個庫:3d

1 M :業務邏輯,能夠用具體邏輯填充code

2 V :頁面,有本身的簡單必要的代碼邏輯,和調用P的代碼 和 實現P規定的接口方法orm

3 P:頁面邏輯,接到V的調用的代碼,並調用M進行邏輯處理和調用V進行頁面反饋blog

4 Models: 公用的實體類

V的代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using P;
using Models;

// 該命名空間下 存放頁面類 
namespace V
{
    public partial class Form1 : Form, IPerson
    {
        private PersonLogic p;
        public Form1()
        {
            InitializeComponent();
            p = new PersonLogic(this);// 這行代碼將MVP 中的V(頁面)和P(頁面邏輯)關聯起來
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            p.ShowPersonListData();
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                p.SelectedIndex(e.Item.Text);
            }
        }


        #region P要回調的方法 接口的實現

        public void ShowMessage(string message)
        {
            MessageBox.Show(message);
        }

        public void ShowPersonList(List<PersonModel> list)
        {
            foreach (var item in list)
            {
                ListViewItem c = listView1.Items.Add(item.Name);
                c.SubItems.Add(item.Age.ToString());
            }
        }

        public void SelectName(string name, string age)
        {
            textBox1.Text = name;
            textBox2.Text = age;
        }

        #endregion
    }
}

 

P的接口代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;


namespace P
{
    /// <summary>
    /// 規定頁面中要實現的方法 會在頁面邏輯類中調用
    /// </summary>
    public interface IPerson
    {
        void ShowMessage(string message);

        void ShowPersonList(List<PersonModel> list);

        void SelectName(string name, string age);

    }
}

P的頁面邏輯代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using M;
using Models;

// 該命名空間下 存放 頁面邏輯
namespace P
{
    public class PersonLogic
    {
        private IPerson view;
        PersonSevise sevice = new PersonSevise();
        public PersonLogic(IPerson iView)
        {
            view = iView;
        }

        public void ShowPersonListData()
        {
           view.ShowPersonList(sevice.list);
        }

        public void SelectedIndex(string name)
        {
           PersonModel model = sevice.list.Where(M => M.Name == name).FirstOrDefault();
           view.SelectName(model.Name, model.Age.ToString());
           view.ShowMessage("選中了");
        }
    }
}

M的邏輯代碼 (數據處理,讀寫數據庫):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;

// 該命名空間下 存放業務邏輯類 實際中 就數據庫和代碼邏輯
namespace M
{
    public class PersonSevise
    {
        public List<PersonModel> list { get; set; }
        public PersonSevise()
        {
            list = new List<PersonModel>();
            list.Add(new PersonModel() { Name = "LXP1", Age = 18 });
            list.Add(new PersonModel() { Name = "LXP2", Age = 19 });
            list.Add(new PersonModel() { Name = "LXP3", Age = 20 });
            list.Add(new PersonModel() { Name = "LXP4", Age = 21 });
            list.Add(new PersonModel() { Name = "LXP5", Age = 22 });
            list.Add(new PersonModel() { Name = "LXP6", Age = 23 });
            list.Add(new PersonModel() { Name = "LXP7", Age = 24 });
            list.Add(new PersonModel() { Name = "LXP8", Age = 25 });
            list.Add(new PersonModel() { Name = "LXP9", Age = 26 });
            list.Add(new PersonModel() { Name = "LXP10", Age = 27 });
           
        }

        public void Add(PersonModel model)
        {
            list.Add(model);
        }
        public void Delete(PersonModel model)
        {
            list.Remove(list.Where(M => M.Equals(model)).FirstOrDefault());
        }
    }
}

Models實體類代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// 該命名空間下存放模型實體類
namespace Models
{
    public class PersonModel : IEquatable<PersonModel>
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public bool Equals(PersonModel other)
        {
            return this.Name == other.Name;
        }
    }
}
相關文章
相關標籤/搜索