工资单解决方案中使用的MVP模式(Windows窗体) [英] MVP Pattern used in a payroll solution (Windows forms)

查看:98
本文介绍了工资单解决方案中使用的MVP模式(Windows窗体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将薪资系统的MVP模式用作我的最后一年项目。在分析的问题中大约有16个实体类(如员工,客户,工资等),我设计的解决方案如下......



没有UI(视图) - 18,

为每个用户界面单独的演示者和模型(18 + 18),

一个数据服务(存储库)迎合所有演示者。

每个视图,模型和数据服务实现接口并使用依赖注入(构造函数Inj)。



我已经实现了一个UI,下面是代码......这段代码编译并运行,提供所需的输出。

我的问题是......



1.在项目结束时应用这种模式有什么后果?还是不好? (因为我没有以前的经验)

2.此模式或代码中的任何大量流量?

3.此模式是否适合于以下项目这个级别?

4.有什么建议改善这个吗?



请注意,我的最终目标是按计划完成项目并通过评估。所以实际上现在我想要的是建立我正在朝着正确的方向发展的信心。

您能否请专家帮助我?我的所有问题的答案都受到欢迎。

谢谢。



查看



I’m using MVP pattern for a payroll system as my final year project. There are about 16 entity classes (like employee, client, salary etc.) in the analyzed problem and I have designed the solution as follows…

No of UIs(Views) – 18,
Separate Presenters and Models for each UI ( 18+18) ,
One Data Service (repository) catering to all presenters.
Each view, model and the data service implement interfaces and use dependency injection (constructor Inj).

Already I have implemented one UI and below is the code… This code compiles and runs giving the desired output.
My questions are…

1. What would be the consequences of applying this pattern, at the end of the project good? or bad? (As I don’t have a previous experience)
2. Any considerable flows in this pattern or in the code?
3. Is this pattern suitable for a project at this level?
4. Any advises to improve this?

Please note that my final ambition is to finish the project with the schedule and to pass the evaluation. So actually now what I want is to build confidence that I’m going the right direction.
Could you experts help me? Answers covering my all questions are mostly welcomed.
Thank you.

View

namespace Payroll
{
    public delegate void ViewEventHandler(object sender, EventArgs e);

    public partial class frmNewClient : Form, IClientView
    {
	//Properties
        public int CID
        {
            get {return Convert.ToInt32(txtCID.Text); }
            set { txtCID.Text=value.ToString();}
        }

        public string ClientName
        {
            get { return txtCName.Text; }
            set { txtCName.Text = value; }
        }
.
.
.

        public frmNewClient()
        {
            InitializeComponent();
        }
        //Raise events, first to update model and then to save data to database
        private void cmdSave_Click(object sender, EventArgs e)
        {
            DataChanged(sender, e);
            Save(sender, e);
        }
	//Even handlers 
        public event EventHandler DataChanged;
        public event EventHandler Save;
        public event EventHandler Search;
        }
}





模特



Model

namespace Payroll
{
    class Client: IClient
    {
	//Properties
        public int CID { get; set; }
        public string ClientName { get; set; }
        public string ClientAdd { get; set; }
        public string ContactPerson { get; set; }
        public string ClientMob { get; set; }
        public string ClientTel { get; set; }
        public string Remarks { get; set; }
        public DateTime EnteredDate { get; set; }
        

        //Custom constructor with parameteres
        public Client(int CID, string clientName, string clientAdd, string contactPerson, string clientMob, string clientTel, string Remarks, DateTime enteredDate)
        {
            this.CID = CID;
            this.ClientName = clientName;
            this.ClientAdd = clientAdd;
            this.ContactPerson = contactPerson;
            this.ClientMob = clientMob;
            this.ClientTel = clientTel;
            this.Remarks = Remarks;
            this.EnteredDate = enteredDate;
        }

        //Default constructor
        public Client()
        {
        }
    }
}





Presenter



Presenter

namespace Payroll
{
    class ClientPresenter
    {
        IClient _model;
        IClientView _view;
        IDataService _ds;

		//Constructor 
		public ClientPresenter( IClient model,IClientView view, IDataService ds)
		{
            		this._ds = ds;
			this._model = model;
			this._view = view;
			this.SetViewPropertiesFromModel();
			this.WireUpViewEvents();
		}
		//This will wire up the custom method provided to event
		private void WireUpViewEvents()
		{
			this._view.DataChanged +=new EventHandler(_view_DataChanged);
            		this._view.Save += new EventHandler(_view_Save);
            		this._view.Search += new EventHandler(_view_Search);
		}

//Following code snippet will sinc the parameters of the view with the parameters of the model and vice versa. (All text boxes in the UI are encapsulated in properties)
//======================================================================================       
		private void SetModelPropertiesFromView()
		{
            foreach (PropertyInfo viewProperty in this._view.GetType().GetProperties())
            {
                if (viewProperty.CanRead)
                {
                    PropertyInfo modelProperty = this._model.GetType().GetProperty(viewProperty.Name);

                    if (modelProperty != null && modelProperty.PropertyType.Equals(viewProperty.PropertyType))
                    {
                        object valueToAssign = Convert.ChangeType(viewProperty.GetValue(this._view, null), modelProperty.PropertyType);

                        if (valueToAssign != null)
                        {
                            modelProperty.SetValue(this._model, valueToAssign, null);
                        }
                    }
                }
            }
		}
		
		private void SetViewPropertiesFromModel()
		{
			foreach (PropertyInfo viewProperty in this._view.GetType().GetProperties())
			{
				if (viewProperty.CanWrite)
				{
					PropertyInfo modelProperty = this._model.GetType().GetProperty(viewProperty.Name);

					if (modelProperty != null && modelProperty.PropertyType.Equals(viewProperty.PropertyType))
					{
						object modelValue = modelProperty.GetValue(this._model, null);

						if (modelValue != null)
						{
							object valueToAssign = Convert.ChangeType(modelValue, viewProperty.PropertyType);

							if (valueToAssign != null)
							{
								viewProperty.SetValue(this._view, valueToAssign, null);
							}
						}
					}
				}
			}
		}
//====================================================================================== 

        private void _view_DataChanged(object sender, EventArgs e)
        {
            this.SetModelPropertiesFromView(); // Updating the model properties when data in the view properties changed…
        }

        private void _view_Save(object sender, EventArgs e)
        {
            this.Save();
        }

        private bool Save()
        {
            return _ds.InsertClient(_model);
        }

        private void _view_Search(object sender, EventArgs e)
        {
            this.Search();
        }

        private void Search()
        {
            var obj = _ds.GetByCID(_model.CID);

            if (obj != null)
            {
                _model = obj;
                this.SetViewPropertiesFromModel(); //Updating the view properties from the model if an object returned from GetByCID() method
            }
        }
    }
}





DataService



DataService

namespace Payroll
{
    class DataService :IDataService
    {
       public bool InsertClient(IClient newClient)
        {
            string InsertStatement = @"BEGIN INSERT INTO client(Client_Name, C_Add, Contact_Person, C_Mob_No, C_Tel_No, Remarks, Ent_Date)" +
                                    " VALUES (@CName, @CAdd, @CPerson, @CMob, @CTel, @Remarks, @entDate) END";

                using (SqlConnection newCon = new SqlConnection(db.GetConnectionString))
                {
                    using (SqlCommand SqlCom = new SqlCommand(InsertStatement,newCon))
                    {
                        try
                        {
                            SqlCom.Parameters.Add("@CName", SqlDbType.VarChar).Value = newClient.ClientName;
                            SqlCom.Parameters.Add("@CAdd", SqlDbType.VarChar).Value = newClient.ClientAdd;
                            SqlCom.Parameters.Add("@CPerson", SqlDbType.VarChar).Value = newClient.ContactPerson;
                            SqlCom.Parameters.Add("@CMob", SqlDbType.Char).Value = newClient.ClientMob;
                            SqlCom.Parameters.Add("@CTel", SqlDbType.Char).Value = newClient.ClientTel;
                            SqlCom.Parameters.Add("@Remarks", SqlDbType.VarChar).Value = newClient.Remarks;
                            SqlCom.Parameters.Add("@entDate", SqlDbType.Date).Value = newClient.EnteredDate;
                            SqlCom.Connection = newCon;
                            newCon.Open();

                            SqlCom.ExecuteNonQuery();
                            return true;
                        }
                        catch (Exception e) { MessageBox.Show(("Error: " + e.Message)); }
                        if (newCon.State == System.Data.ConnectionState.Open) newCon.Close();
                        return false;
                    }
                }
        }

        public Client GetByCID(int CID)
        {
            string SelectStatement=@"SELECT * FROM client WHERE CID=@ID";

                using (SqlConnection NewCon = new SqlConnection(db.GetConnectionString))
                {                    
                    using (SqlCommand SqlCom = new SqlCommand(SelectStatement,NewCon))
                    {
                        SqlCom.Parameters.Add("@ID", SqlDbType.Char).Value = CID;

                        try
                        {
                            NewCon.Open();
                            using (SqlDataReader NewRdr = SqlCom.ExecuteReader())
                            {

                                if (NewRdr.Read())
                                {
                                    return new Client
                                    {
                                        CID = NewRdr.GetInt32(0),
                                        ClientName = NewRdr.GetString(1),
                                        ClientAdd = NewRdr.GetString(2),
                                        ContactPerson = NewRdr.GetString(3),
                                        ClientMob = NewRdr.GetString(4),
                                        ClientTel = NewRdr.GetString(5),
                                        Remarks = NewRdr.GetString(6),
                                        EnteredDate = NewRdr.GetDateTime(7)
                                    };
                                }
                                return null;
                            }
                        }
                        catch (Exception e) { MessageBox.Show(("Error: " + e.Message)); }
                        if (NewCon.State == System.Data.ConnectionState.Open) NewCon.Close();
                        return null;                        
                    }                                 
                }
        }
        }
}





计划



Programme

private void btnClient_Click(object sender, EventArgs e)
{
    IClient Client = new Client();
    IClientView ClientView = new frmNewClient();
    IDataService ds = new DataService();

    new ClientPresenter(Client, ClientView, ds);
    ClientView.ShowDialog();
}

推荐答案

这篇关于工资单解决方案中使用的MVP模式(Windows窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆