Is this a bad design?

J

John Jackson

Hello all,

I have written a user control for the purpose of displaying certain
data from a data source. Instead of having the control to have access
to the data source directly, the control will raise an event to the
hosting page whenever it needs to refresh the data (e.g. user choose a
different set of rows) and the hosting page will fetch the appropriate
data from the data source. The data will then be pass back to the
control (in the form of an array of structs) for display.

However, instead of having a function in the user control for the
hosting page to call to load data, I have an "out" parameter as one of
the event parameters. The host page will assign the newly fetched data
to this parameter.

Is that a bad design? If so, why?

Thanks for all your input in advance.


-jj
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

John said:
Hello all,

I have written a user control for the purpose of displaying certain
data from a data source. Instead of having the control to have access
to the data source directly, the control will raise an event to the
hosting page whenever it needs to refresh the data (e.g. user choose a
different set of rows) and the hosting page will fetch the appropriate
data from the data source. The data will then be pass back to the
control (in the form of an array of structs) for display.

However, instead of having a function in the user control for the
hosting page to call to load data, I have an "out" parameter as one of
the event parameters. The host page will assign the newly fetched data
to this parameter.

Is that a bad design? If so, why?

The usual way of sending data to and from an event handler, is to create
a custom EventArgs class where you put the data.

Which one of the designs that are better is hard to say. If you use an
out parameter, it's obvious how the data flows, but on the other hand
people will recognise the use of an EventArgs class as it then looks
like event handlers usually do.

The out and ref keywords are mostly used in procedural programming,
rarely in object oriented programming. It's used sometimes in the
framwork for performance reasons (to avoid creating an object), but that
kind of optimising doesn't really apply here.
 
T

Tom Dacon

There's nothing preventing you from making your event delegate a function
instead of a sub (if you're programming in VB) or returning an array of
structs instead of void (if you're in C#). I don't think that's well known.

Tom Dacon
Dacon Software Consulting
 
B

Bruce Wood

Hello all,

I have written a user control for the purpose of displaying certain
data from a data source. Instead of having the control to have access
to the data source directly, the control will raise an event to the
hosting page whenever it needs to refresh the data (e.g. user choose a
different set of rows) and the hosting page will fetch the appropriate
data from the data source. The data will then be pass back to the
control (in the form of an array of structs) for display.

However, instead of having a function in the user control for the
hosting page to call to load data, I have an "out" parameter as one of
the event parameters. The host page will assign the newly fetched data
to this parameter.

Is that a bad design? If so, why?

I believe so. I believe that if someday more than one subscriber
subscribes to the event, then the results are undetermined.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Bruce said:
I believe so. I believe that if someday more than one subscriber
subscribes to the event, then the results are undetermined.

Good point. The result would depend on the order of the event handlers,
and an event doesn't have a mechanism for controlling the order of the
event handlers.
 
J

Jon Skeet [C# MVP]

Göran Andersson said:
Good point. The result would depend on the order of the event handlers,
and an event doesn't have a mechanism for controlling the order of the
event handlers.

I'm not sure I'd go along with the last bit. Any event which is
implemented using a simple backing delegate, implementing add with +=
and remove with -= (corresponding to Delegate.Combine and
Delegate.Remove respectively) will run the delegates in the order in
which they were added. Delegate.Combine is quite specific about its
effects.

However, I'd agree that relying on the order in which event handlers
are added could lead to fragility.
 
S

Sir C4

Hello all,

I have written a user control for the purpose of displaying certain
data from a data source. Instead of having the control to have access
to the data source directly, the control will raise an event to the
hosting page whenever it needs to refresh the data (e.g. user choose a
different set of rows) and the hosting page will fetch the appropriate
data from the data source. The data will then be pass back to the
control (in the form of an array of structs) for display.

However, instead of having a function in the user control for the
hosting page to call to load data, I have an "out" parameter as one of
the event parameters. The host page will assign the newly fetched data
to this parameter.

Is that a bad design? If so, why?

Thanks for all your input in advance.

-jj

I tend to want to encapsulate all functionality into the user control
so it can be dropped onto ANY page with little or no modification on
the parent page to make the control work.
 
B

Bruce Wood

I tend to want to encapsulate all functionality into the user control
so it can be dropped onto ANY page with little or no modification on
the parent page to make the control work.

I tend to design user controls assuming that it is the job of the
parent control do perform coordination. To me it is tremendously
important that the user control not be aware of its environment, so in
a way I agree with your statement.

However, in my case, there are modifications to the parent control: it
is the one that must subscribe to events in the user control and call
the user control's methods as appropriate to make things happen.

The OP's design achieves this, but I wouldn't use an out parameter on
an event handler. I would just provide a method on the user control
that the parent could use in order to populate it with data to
display.
 

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