This is my first attempt at a Model-View-Presenter pattern Winforms application. The model is a class
that stores a bunch of primitives, and the view is a Form
that provides a ListBox
for selecting individual Data
items, whose members are then displayed in text boxes.
DataModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
namespace ModelViewPresenter
{
public static class RandomExtensions
{
public static decimal NextDecimal(this Random random)
{
byte scale = (byte)random.Next(29);
bool sign = random.Next(2) == 1;
return new decimal(
random.NextInt32(),
random.NextInt32(),
random.NextInt32(),
sign,
scale);
}
public static int NextInt32(this Random random)
{
unchecked
{
int firstBits = random.Next(0, 1 << 4) << 28;
int lastBits = random.Next(0, 1 << 28);
return firstBits | lastBits;
}
}
public static string NextString(this Random random, int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return new string(Enumerable.Repeat(chars, length).Select(s =>
s[random.Next(s.Length)]).ToArray());
}
}
public class DataModel : IDataModel
{
private static int NextId = 1;
private static Random random = new Random();
public bool Bool { get; set; }
public byte Byte { get; set; }
public char Char { get; set; }
public IList<DataModel> Data
{
get
{
var data = new List<DataModel>();
for (int i = 0; i < 10; i++)
{
data.Add(RandomData());
}
return data;
}
}
public decimal Decimal { get; set; }
public double Double { get; set; }
public float Float { get; set; }
public int Id { get; set; }
public int Int { get; set; }
public long Long { get; set; }
public object Object { get; set; }
public short Short { get; set; }
public string String { get; set; }
private static DataModel RandomData()
{
return new DataModel
{
Id = NextId++,
Bool = Convert.ToBoolean(random.Next(2)),
Byte = Convert.ToByte(random.Next(Byte.MaxValue)),
Char = Convert.ToChar(random.Next(Char.MaxValue)),
Decimal = random.NextDecimal(),
Double = random.NextDouble() * Double.MaxValue,
Float = (float)random.NextDouble() * float.MaxValue,
Int = random.NextInt32(),
Long = random.Next() << 32 | random.Next(),
Object = new int[] { random.Next(), random.Next() },
Short = (short)random.Next(short.MaxValue),
String = random.NextString(32)
};
}
}
}
IDataModel.cs
using System.Collections.Generic;
namespace ModelViewPresenter
{
internal interface IDataModel
{
IList<DataModel> Data { get; }
}
}
DataView.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ModelViewPresenter
{
public partial class DataView : Form, IDataView
{
public DataView()
{
InitializeComponent();
BindComponent();
}
public event Action DataSelected;
public IList<DataModel> Data
{
get
{
return this.dataListBox.DataSource as IList<DataModel>;
}
}
public DataModel SelectedData
{
get
{
return this.dataListBox.SelectedItem as DataModel;
}
}
public void LoadData(IList<DataModel> data)
{
this.dataListBox.DataSource = data;
}
public void LoadData(DataModel data)
{
this.boolTextBox.Text = data.Bool.ToString();
this.byteTextBox.Text = data.Byte.ToString();
this.charTextBox.Text = data.Char.ToString();
this.decimalTextBox.Text = data.Decimal.ToString();
this.doubleTextBox.Text = data.Double.ToString();
this.floatTextBox.Text = data.Float.ToString();
this.intTextBox.Text = data.Int.ToString();
this.longTextBox.Text = data.Long.ToString();
this.objectTextBox.Text = data.Object.ToString();
this.shortTextBox.Text = data.Short.ToString();
this.stringTextBox.Text = data.String.ToString();
}
private void BindComponent()
{
this.dataListBox.DisplayMember = "Id";
this.dataListBox.SelectedIndexChanged +=
OnDataListBoxSelectedIndexChanged;
}
private void OnDataListBoxSelectedIndexChanged(
object sender, EventArgs e)
{
if (this.DataSelected != null)
{
this.DataSelected();
}
}
}
}
IDataView.cs
using System;
using System.Collections.Generic;
namespace ModelViewPresenter
{
public interface IDataView
{
event Action DataSelected;
IList<DataModel> Data { get; }
DataModel SelectedData { get; }
void LoadData(IList<DataModel> data);
void LoadData(DataModel data);
}
}
DataPresenter.cs
namespace ModelViewPresenter
{
internal class DataPresenter
{
private IDataModel model;
private IDataView view;
public DataPresenter(IDataModel model, IDataView view)
{
this.model = model;
this.view = view;
this.view.DataSelected += OnDataSelected;
this.view.LoadData(model.Data);
}
public void OnDataSelected()
{
if (this.view.SelectedData != null)
{
this.view.LoadData(this.view.SelectedData);
}
}
}
}
Program.cs
using System;
using System.Windows.Forms;
namespace ModelViewPresenter
{
internal static class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var model = new DataModel();
var view = new DataView();
new DataPresenter(model, view);
Application.Run(view);
}
}
}