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

Passive View implementation in c# MVP

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

Problem

I have a c# WinForms application in which I have tried to implement the Model View Presenter pattern with Passive View. The View derives from a parent IView which propagates the public members and events of the View to its Presenter. In this way, the Presenter can utilize these field and react to the events without relying on the implementation of the View (forms library and such). The View only fires events and performs input validation logic.

I have included one of my Views below. I was wondering if this View is 'Passive' enough, so that my program is compliant with the pattern and if my general idea of the pattern is correct. Any other improvements are welcome as well. This is my first ever application, c# experiece, and implementation of the MVP pattern so bear with me.

View

```
public partial class StandardScreenView : Form, IStandardScreenView
{
// Public members.
public List QuestionStandards
{
get { return currentQuestionStandards.Items.Cast().ToList(); }
set
{
currentQuestionStandards.DataSource = value;
currentQuestionStandards.SelectedItem = null;
}
}

public List MaturityStandards
{
get { return currentMaturityStandards.Items.Cast().ToList(); }
set
{
currentMaturityStandards.DataSource = value;
currentMaturityStandards.SelectedItem = null;
}
}
public List ComplianceStandards
{
get { return currentComplianceStandards.Items.Cast().ToList(); }
set
{
currentComplianceStandards.DataSource = value;
currentComplianceStandards.SelectedItem = null;
}
}
public string SelectedQuestionStandard
{
get
{
if(currentQuestionStandards != null)
{
return currentQuestionStandards.SelectedItem.ToString();
}
return null;
}
set { currentQuestionStandards.SelectedItem =

Solution

event EventHandler InitializingStandards;
event EventHandler AddingQuestionStandard;


This is not exactly how we implement events.

The EventHandler Delegate already works with EventArgs so it's redundant to use the generic one:

public delegate void EventHandler(object sender, EventArgs e)


it's the same as if you wrote:

event EventHandler InitializingStandards;


but if you use the generic overload then the argument should be derived from EventArgs:

public delegate void EventHandler(object sender, TEventArgs e)


The standard signature of an event handler delegate defines a method that does not return a value. This method's first parameter is of type Object and refers to the instance that raises the event. Its second parameter is derived from type EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply the value of the EventArgs.Empty field. Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data.

EventHandler Delegate

(Emphasis mine)

For some strange reason the delegate does not enforce that and has no where TEventArgs : EventArgs constraint. But if you want to stick to the convention then you should create new types like:

class AddingQuestionStandardEventArgs : EventArgs
{
    AddingQuestionStandardEventArgs(string standardName)
    {
         StandardName = standardName;
    }
    public string StandardName { get; }
}


to use them instead of the string:

event EventHandler AddingQuestionStandard;

Code Snippets

event EventHandler<EventArgs> InitializingStandards;
event EventHandler<string> AddingQuestionStandard;
public delegate void EventHandler(object sender, EventArgs e)
event EventHandler InitializingStandards;
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e)
class AddingQuestionStandardEventArgs : EventArgs
{
    AddingQuestionStandardEventArgs(string standardName)
    {
         StandardName = standardName;
    }
    public string StandardName { get; }
}

Context

StackExchange Code Review Q#146036, answer score: 4

Revisions (0)

No revisions yet.