Using generic EventArgs

A

Andrus

I need to validate document DataGridview rows. Rows can be different types:
invoice rows, order rows etc.
I created generic EventArgs type for this:

public class CellValidatedEventArgs<TRow> : EventArgs
where TRow : class , IRow
{
public string PropertyName;
public BindingList<TRow> Rows;

public CellValidatedEventArgs(BindingList<TRow> rows, string
propertyName)
{
Rows = rows;
PropertyName = propertyName;
}
}


My entity base class contains empty method to allow override :

class EntityBase {
public virtual void OnRowCellValidated<TRow>(TRow sender,
CellValidatedEventArgs<TRow> e)
where TRow : class, IRow { }
}

Is this best design pattern ?

Andrus,
 
I

Ignacio Machin ( .NET/ C# MVP )

I need to validate document DataGridview rows. Rows can be different types:
invoice rows, order rows etc.
I created generic EventArgs type for this:

public class CellValidatedEventArgs<TRow> : EventArgs
          where TRow : class , IRow
    {
        public string PropertyName;
        public BindingList<TRow> Rows;

        public CellValidatedEventArgs(BindingList<TRow> rows, string
propertyName)
        {
            Rows = rows;
            PropertyName = propertyName;
        }
    }

My entity base class contains empty method to allow override :

class EntityBase {
        public virtual void OnRowCellValidated<TRow>(TRow sender,
CellValidatedEventArgs<TRow> e)
                         where TRow : class, IRow { }

}

Is this best design pattern ?

Andrus,

Hi,

Who is going to raise the events?

I think that the design is good in principle.
Where does EntityBase fit though ?
I would think that it;s your base class for the different type of
columns, but if that is the case I would say: where TRow :
EntityBase , IRow
 
A

Andrus

Who is going to raise the events?

Events are raised by DataGridView overridden OnCellValidated method.
However code uses row entity as sender because:

1. Code can use sender. without casting
2. Allow to use same pattern for Textbox OnValidated call
3. Allow to use type inference for TRow. Probably caller can simply use

OnRowCellValidated( rowEntity, ...

without specifying type parameter.

No idea is it good pattern or should I use "object sender" as sender
and pass real sender (DataGridview) to method and entity as
CellValidatedEventArgs class property:

public class CellValidatedEventArgs<TRow> : EventArgs
where TRow : EntityBase, IRow
{
public TRow Entity;
....

In this case sender parameter is almost never used and "e.Entity" shoud
be used instead if sender to access entity properties.

Those methods will check if user has specified custom script.
In this case its creates validation class from this script. Script overrides
validation method.
If there is no script, validation type if created from base validation type.

Where does EntityBase fit though ?
I would think that it;s your base class for the different type of
columns, but if that is the case I would say: where TRow :
EntityBase , IRow

Yes, row entities inherist also from Entitybase. Also, Validation class does
not inherit from EntityBase class since every grid column can have custom
validation script created by user.

So correct sample code is:

public class CellValidatedEventArgs<TRow> : EventArgs
where TRow : EntityBase, IRow
{
public string PropertyName;
public BindingList<TRow> Rows;

public CellValidatedEventArgs(BindingList<TRow> rows, string
propertyName)
{
Rows = rows;
PropertyName = propertyName;
}
}


Validation base class contains empty method to allow override :

class ValidationBase {
public virtual void OnRowCellValidated<TRow>(TRow sender,
CellValidatedEventArgs<TRow> e)
where TRow : EntityBase, IRow { }
}

Andrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top