Example of an invalid model

This commit is contained in:
Tracy Pearson
2024-04-02 23:39:30 -04:00
parent 411b5722e2
commit 4deedd3339
12 changed files with 441 additions and 1 deletions

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVPLearning.BaseLibrary
{
public class BaseButton : Button
{
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVPLearning.BaseLibrary
{
public class BaseLabel : Label
{
}
}

View File

@@ -37,6 +37,7 @@
testAreaToolStripMenuItem = new ToolStripMenuItem();
bindToModelToolStripMenuItem = new ToolStripMenuItem();
bindToViewToolStripMenuItem = new ToolStripMenuItem();
validateModelToolStripMenuItem = new ToolStripMenuItem();
menuStrip1.SuspendLayout();
SuspendLayout();
//
@@ -86,7 +87,7 @@
//
// testAreaToolStripMenuItem
//
testAreaToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { bindToModelToolStripMenuItem, bindToViewToolStripMenuItem });
testAreaToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { bindToModelToolStripMenuItem, bindToViewToolStripMenuItem, validateModelToolStripMenuItem });
testAreaToolStripMenuItem.Name = "testAreaToolStripMenuItem";
testAreaToolStripMenuItem.Size = new Size(66, 20);
testAreaToolStripMenuItem.Text = "Test Area";
@@ -105,6 +106,13 @@
bindToViewToolStripMenuItem.Text = "Bind To View";
bindToViewToolStripMenuItem.Click += BindToViewToolStripMenuItem_Click;
//
// validateModelToolStripMenuItem
//
validateModelToolStripMenuItem.Name = "validateModelToolStripMenuItem";
validateModelToolStripMenuItem.Size = new Size(180, 22);
validateModelToolStripMenuItem.Text = "Validate Model";
validateModelToolStripMenuItem.Click += ValidateModelToolStripMenuItem_Click;
//
// MainView
//
AutoScaleDimensions = new SizeF(7F, 15F);
@@ -132,5 +140,6 @@
private ToolStripMenuItem testAreaToolStripMenuItem;
private ToolStripMenuItem bindToModelToolStripMenuItem;
private ToolStripMenuItem bindToViewToolStripMenuItem;
private ToolStripMenuItem validateModelToolStripMenuItem;
}
}

View File

@@ -1,5 +1,6 @@
using MVPLearning.RecordKeeping.SermonFiler;
using MVPLearning.TestingArea.ModelProperties;
using MVPLearning.TestingArea.Validation;
using MVPLearning.TestingArea.ViewProperties;
namespace MVPLearning
@@ -30,5 +31,10 @@ namespace MVPLearning
{
_ = new ViewPropertiesPresenter(new ViewPropertiesView() { MdiParent = this });
}
private void ValidateModelToolStripMenuItem_Click(object sender, EventArgs e)
{
_ = new ValidateController(new ValidateForm() { MdiParent = this });
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVPLearning.TestingArea
{
static class AreaStrings
{
public static string LastNameEmpty {get; }= "Last name cannot be empty.";
}
}

View File

@@ -0,0 +1,11 @@

namespace MVPLearning.TestingArea.Validation
{
public interface IValidateForm
{
event EventHandler<ValidationModel>? Save;
void Invalid(ValidateInvalid model);
void LoadForm(ValidationModel model);
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVPLearning.TestingArea.Validation
{
public class ValidateController
{
private readonly IValidateForm _form;
public ValidateController(IValidateForm form) {
_form = form;
_form.Save += Save;
_form.LoadForm(new() { Id = 0, Firstname = "John", Lastname = "Doe" });
if (_form is Form window) { window.Show(); }
}
private void Save(object? sender, ValidationModel e)
{
if (e == null || string.IsNullOrEmpty(e.Lastname)) { _form.Invalid(new()); return; }
ValidationModel model = new() { Firstname = e.Firstname, Lastname = e.Lastname, Id = e.Id };
_form.LoadForm(model);
}
}
}

View File

@@ -0,0 +1,121 @@
namespace MVPLearning.TestingArea.Validation
{
partial class ValidateForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
lblFirstname = new BaseLibrary.BaseLabel();
txtFirstname = new BaseLibrary.BaseTextBox();
txtLastname = new BaseLibrary.BaseTextBox();
lblLastname = new BaseLibrary.BaseLabel();
btnSave = new BaseLibrary.BaseButton();
baseButton1 = new BaseLibrary.BaseButton();
SuspendLayout();
//
// lblFirstname
//
lblFirstname.AutoSize = true;
lblFirstname.Location = new Point(12, 9);
lblFirstname.Name = "lblFirstname";
lblFirstname.Size = new Size(65, 15);
lblFirstname.TabIndex = 0;
lblFirstname.Text = "First name:";
//
// txtFirstname
//
txtFirstname.BorderStyle = BorderStyle.FixedSingle;
txtFirstname.Location = new Point(83, 7);
txtFirstname.MaxLength = 20;
txtFirstname.Name = "txtFirstname";
txtFirstname.Size = new Size(155, 23);
txtFirstname.TabIndex = 1;
//
// txtLastname
//
txtLastname.BorderStyle = BorderStyle.FixedSingle;
txtLastname.Location = new Point(83, 36);
txtLastname.MaxLength = 20;
txtLastname.Name = "txtLastname";
txtLastname.Size = new Size(155, 23);
txtLastname.TabIndex = 3;
//
// lblLastname
//
lblLastname.AutoSize = true;
lblLastname.Location = new Point(12, 38);
lblLastname.Name = "lblLastname";
lblLastname.Size = new Size(64, 15);
lblLastname.TabIndex = 2;
lblLastname.Text = "Last name:";
//
// btnSave
//
btnSave.Location = new Point(163, 65);
btnSave.Name = "btnSave";
btnSave.Size = new Size(75, 23);
btnSave.TabIndex = 4;
btnSave.Text = "Save";
btnSave.UseVisualStyleBackColor = true;
btnSave.Click += Save_Click;
//
// baseButton1
//
baseButton1.Location = new Point(83, 65);
baseButton1.Name = "baseButton1";
baseButton1.Size = new Size(75, 23);
baseButton1.TabIndex = 5;
baseButton1.Text = "Test";
baseButton1.UseVisualStyleBackColor = true;
baseButton1.Click += Test_Click;
//
// ValidateForm
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(331, 174);
Controls.Add(baseButton1);
Controls.Add(btnSave);
Controls.Add(txtLastname);
Controls.Add(lblLastname);
Controls.Add(txtFirstname);
Controls.Add(lblFirstname);
Name = "ValidateForm";
Text = "ValidateForm";
ResumeLayout(false);
PerformLayout();
}
#endregion
private BaseLibrary.BaseLabel lblFirstname;
private BaseLibrary.BaseTextBox txtFirstname;
private BaseLibrary.BaseTextBox txtLastname;
private BaseLibrary.BaseLabel lblLastname;
private BaseLibrary.BaseButton btnSave;
private BaseLibrary.BaseButton baseButton1;
}
}

View File

@@ -0,0 +1,63 @@
using MVPLearning.BaseLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MVPLearning.TestingArea.Validation
{
public partial class ValidateForm : BaseForm, IValidateForm
{
private readonly ValidationModel _model = new();
private ValidationModel? _loadedModel;
public ValidateForm()
{
InitializeComponent();
_model.PropertyChanged += ModelChanged;
txtFirstname.Bind(_model, nameof(_model.Firstname));
txtLastname.Bind(_model, nameof(_model.Lastname));
}
private void ModelChanged(object? sender, PropertyChangedEventArgs e)
{
EnableSave();
}
private void EnableSave()
{
bool enable = !_model.Equals(_loadedModel);
btnSave.Enabled = enable;
}
public event EventHandler<ValidationModel>? Save;
public void LoadForm(ValidationModel model)
{
_loadedModel = model;
_model.Firstname = model.Firstname;
_model.Lastname = model.Lastname;
_model.Id = model.Id;
EnableSave();
}
public void Invalid(ValidateInvalid model)
{
if (model.Lastname != null)
{
txtLastname.Focus();
MessageBox.Show(model.Lastname, Text);
}
}
private void Save_Click(object sender, EventArgs e)
{
Save?.Invoke(this, _model);
}
private void Test_Click(object sender, EventArgs e)
{
_model.Firstname = "Test";
}
}
}

View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVPLearning.TestingArea.Validation
{
public class ValidateInvalid
{
public string Lastname { get; set; } = AreaStrings.LastNameEmpty;
}
}

View File

@@ -0,0 +1,32 @@
using MVPLearning.Structure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVPLearning.TestingArea.Validation
{
public class ValidationModel : ObservableObject
{
private string _firstname = string.Empty;
private string _lastname = string.Empty;
private int _id;
public string Firstname { get { return _firstname; } set { SetProperty(ref _firstname, value); } }
public string Lastname { get { return _lastname; } set { SetProperty(ref _lastname, value); } }
public int Id { get { return _id; } set { SetProperty(ref _id, value); } }
public override bool Equals(object? obj)
{
return obj is ValidationModel model &&
Id == model.Id &&
Firstname == model.Firstname &&
Lastname == model.Lastname;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}