C# Winforms应用程序,具有基本MVP模式,使用EF和SQL后端
我正在编写一个应用程序,在阅读并遵循许多示例之后,我实现了一个基本的MVP模式。在简单的维护屏幕上,它工作得非常好;然而,在更复杂的屏幕上,模式并不那么好 下面是一个带有接口的短代码块,以及通过LINQ获取一些数据的第一种方法。演示者都是独立于模型的,每个模型POCO都是通过EF codefirst生成的C# Winforms应用程序,具有基本MVP模式,使用EF和SQL后端,c#,winforms,oop,mvp,C#,Winforms,Oop,Mvp,我正在编写一个应用程序,在阅读并遵循许多示例之后,我实现了一个基本的MVP模式。在简单的维护屏幕上,它工作得非常好;然而,在更复杂的屏幕上,模式并不那么好 下面是一个带有接口的短代码块,以及通过LINQ获取一些数据的第一种方法。演示者都是独立于模型的,每个模型POCO都是通过EF codefirst生成的 public interface ILocnMastView { int LocnMastID { get; set; } Int16 LocnSubT
public interface ILocnMastView
{
int LocnMastID { get; set; }
Int16 LocnSubTypeID { get; set; }
event EventHandler<EventArgs> FetchLocnMasts;
//Event to fire a 'fetch' of a row of data
event EventHandler<EventArgs> RefreshLocnMast;
}
public interface ILocnMastData : ILocnMastView
{
//Event to fire a record INSERT
event EventHandler<EventArgs> InsertLocnMast;
//Event to fire a record UPDATE
event EventHandler<EventArgs> UpdateLocnMast;
}
public class PresenterLocnMast
{
#region Member Variables
private ILocnMastView _viewfetch; //The view (form) instance that this presenter interface will use
private ILocnMastData _viewdata; //The view (form) instance that this inherited interface will use
private LOCNMast _obj = new LOCNMast(); //A single row of data to be returned out of this class
private List<LOCNMast> _data = null; //List of supplycodes to be returned out of this class
private List<LocnMastObject> _dataobject; //List of contacts joined to persontype table in a specialised class object
private string _message = string.Empty;
#endregion Member Variables
#region Member Properties
public string Message
{
get { return _message; }
set { _message = value; }
}
public LOCNMast Locnmast
{
get { return _obj; }
set { _obj = value; }
}
public List<LOCNMast> LocnMasts
{
get { return _data; }
set { _data = value; }
}
public List<LocnMastObject> LocnMastObjects
{
get { return _dataobject; }
set { _dataobject = value; }
}
#endregion Member Properties
#region Member Objects
public class LocnMastObject
{
public int ID { get; set; }
public string combinedtext { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string LocationType { get; set; }
public string Warehouse { get; set; }
public string Category { get; set; }
public DateTime LastEditedDate { get; set; }
public string LastEditedBy { get; set; }
public Boolean Active { get; set; }
}
#endregion Member Objects
#region Init
public PresenterLocnMast(ILocnMastView viewfetch, ILocnMastData viewdata)
{
this._viewfetch = viewfetch;
this._viewdata = viewdata;
this.Initialise();
}
private void Initialise()
{
if (_viewdata != null)
{
this._viewdata.UpdateLocnMast += new EventHandler<EventArgs>(UpdateLocnMast);
this._viewdata.InsertLocnMast += new EventHandler<EventArgs>(InsertLocnMast);
}
this._viewfetch.FetchLocnMasts += new EventHandler<EventArgs>(FetchLocnMasts);
this._viewfetch.RefreshLocnMast += new EventHandler<EventArgs>(RefreshLocnMast);
}
#endregion Init
#region Methods
/// <summary>
/// Routine: FetchLocnMasts
/// Purpose: Event to Fetch all details from the database by locnsubtype
/// Author: Frustrated Winforms Developer
/// Date: 13/11/2015
/// </summary>
private void FetchLocnMasts(object sender, EventArgs e)
{
try
{
using (var db = new DevDBContext())
var queryforobject = from l in db.LOCNMasts.AsNoTracking()
join t in db.LocnTypes.AsNoTracking() on l.LocationTypeID equals t.ID
join w in db.WHMasts.AsNoTracking() on l.WarehouseID equals w.ID
join c in db.LocnCategoryTypes.AsNoTracking() on l.LocationCategoryTypeID.Value equals c.ID into pp
from LocationCategoryTypeID in pp.DefaultIfEmpty()
orderby l.Name
//Put the query results into the bespoke object
select new LocnMastObject { ID = l.ID,
Name = l.Name,
Description = l.Description,
combinedtext = l.Name + " - " + l.Description,
LocationType = t.Description,
Warehouse = w.Description,
Category = LocationCategoryTypeID.Name,
LastEditedBy = l.LastEditedBy,
LastEditedDate = l.LastEditedDate,
Active = l.Active };
if (queryforobject.Count() > 0)
_dataobject = queryforobject.ToList();
}
}
catch (Exception ex)
{
//HandleError
}
}
正如您所看到的,尽管数据的获取非常简单,但设置所有内容都需要大量代码。我一定是遗漏了一些东西,使得表单后面的代码少了很多,这肯定是重点吗
很抱歉发了这么长的帖子,欢迎所有反馈 我认为您可能需要将其归结为更小的代码示例和问题陈述。对我来说,这是一个值得关注的问题,我将继续前进。其他人可能会投票决定关闭,因为范围太广。另一个可能的密切原因是:没有明确问题陈述的问题对其他读者没有用处。请看:。我知道它很长,但我觉得如果不提供完整的示例,我无法说明这个问题
public partial class frmConfigLocation : Telerik.WinControls.UI.RadForm, IWHMastView, ILocnCategoryTypeView, ILocnMastData
#region Member Variables
private Int16 _locationcategorytypeid = -1;
private Int16 _whmastid = -1;
private int _locnmastid = -1;
private Int16 _locnsubtypeid = -1;
private PresenterWHMast _presenterwhmast;
private PresenterLocnCategoryType _presenterlocncategorytype;
private PresenterLocnMast _presenterlocnmast;
#endregion Member Variables
#region Member Events
public event EventHandler<EventArgs> FetchLocnCategoryTypes;
public event EventHandler<EventArgs> RefreshLocnCategoryType;
public event EventHandler<EventArgs> FetchWHMasts;
public event EventHandler<EventArgs> RefreshWHMast;
public event EventHandler<EventArgs> FetchLocnMasts;
public event EventHandler<EventArgs> RefreshLocnMast;
public event EventHandler<EventArgs> UpdateLocnMast;
public event EventHandler<EventArgs> InsertLocnMast;
#endregion Member Events
public Int16 LocnCategoryTypeID
{
get { return _locationcategorytypeid; }
set { _locationcategorytypeid = LocnCategoryTypeID; }
}
public Int16 WHMastID
{
get { return _whmastid; }
set { _whmastid = WHMastID; }
}
public int LocnMastID
{
get { return _locnmastid; }
set { _locnmastid = LocnMastID; }
}
#endregion Member Properties
#region FormMethods
public frmConfigLocation()
{
this.FormClosed += new FormClosedEventHandler(frmLocation_FormClosed);
InitializeComponent();
_presenterlocnmast = new PresenterLocnMast(this, this);
_presenterlocncategorytype = new PresenterLocnCategoryType(this, null);
_presenterwhmast = new PresenterWHMast(this, null);
}
private void FetchData([Optional] Boolean postsave)
{
try
{
this.Cursor = Cursors.WaitCursor;
if (FetchLocnMasts != null)
{
FetchLocnMasts(this, EventArgs.Empty);
radGridViewLocnAll.DataSource = _presenterlocnmast.LocnMastObjects;
//Hide the ID column of the radgrid
GlobalControlMethods.radGridViewColumnVisible(radGridViewLocnAll, 0, false);
radGridViewLocnAll.CurrentRow = null;
}