HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

PriceDialog - Prompt & price validation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pricepromptvalidationpricedialog

Problem

In a recent CodeReview I presented a Product Manager that manages product listings and prices. Based on the great feedback I have refactored the mentioned Prompt and validator class and want feedback about the code and naming.

Utilities/ProductValidator.cs

namespace Ferienaufgabe_2.Utilities
{
class ProductValidator
{
    public bool IsValidPriceFormat(string value)
    {
        decimal result;
        return (decimal.TryParse(value, out result) && result >= 0 );
    }
}
}


View/PriceDialog.cs

```
using Ferienaufgabe_2.Utilities;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Ferienaufgabe_2.View
{
class PriceDialog
{
private ProductValidator ProductValidator { get; set; }
private Form DialogBox { get; set; }
private Label TextLabel { get; set; }
private TextBox InputBox { get; set; }
private Button ButtonConfirmation { get; set; }

private const string DefaultTextInputBox = "z.B.: 10,00";
private const string MessageInvalidPriceFormat = "Ungültige Preiseingabe! Z.B.: 10,00";
private const string TextOfLabel = "Preis:";
private const string TextOfConfirmButton = "Prozess abschliessen";

public PriceDialog()
{
this.InitializeComponent();
this.DesignComponent();
this.AddEventsToComponent();
this.AddComponentToDialog();
}

private void InitializeComponent()
{
this.ProductValidator = new ProductValidator();
this.DialogBox = new Form();
this.TextLabel = new Label();
this.InputBox = new TextBox();
this.ButtonConfirmation = new Button();
}

private void DesignComponent()
{
this.DialogBox.Width = 300;
this.DialogBox.Height = 120;
this.DialogBox.StartPosition = FormStartPosition.CenterParent;
this.DialogBox.FormBorderStyle = FormBorderStyle.None;

this.TextLabel.Location = new Point(110, 15);
this.TextLabel.Text = TextOfLabel;
this.TextLabel.F

Solution

class PriceDialog
{
    private ProductValidator ProductValidator { get; set; }
    private Form DialogBox { get; set; }
    private Label TextLabel { get; set; }
    private TextBox InputBox { get; set; }
    private Button ButtonConfirmation { get; set; }


Not quite how MVC works. Your view should implement an interface. That interface won't expose anything from the UI. It should only expose the data you need to work with, not UI controls.

class PriceDialog : IPriceView
{
    string SomeText 
    {
        get { return TextBox.Text; }
        set { TextBox.Text = value; }
    }

    //...

}


Then a presenter or controller, depending on the flavor of MVC, interacts with the view and responds to its events. This makes your code testable, but there are other benefits. It allows you to easily swap out one view for another without changing the controller. As it is right now, you'd have to duplicate a lot of this logic in order to implement a console interface. By making your view anemic and interacting through an interface, you decouple the concept of a PriceView from this particular implementation of it.

Code Snippets

class PriceDialog
{
    private ProductValidator ProductValidator { get; set; }
    private Form DialogBox { get; set; }
    private Label TextLabel { get; set; }
    private TextBox InputBox { get; set; }
    private Button ButtonConfirmation { get; set; }
class PriceDialog : IPriceView
{
    string SomeText 
    {
        get { return TextBox.Text; }
        set { TextBox.Text = value; }
    }

    //...

}

Context

StackExchange Code Review Q#96409, answer score: 3

Revisions (0)

No revisions yet.