473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

events that fire events that fire events....a bad thing?

Hi,

I have a scenario where a class is wrapped inside another but the inner
class triggers an event. I want that event to be accessed outside of the
wrapper. As a result i do code such as:

public Table()
{
_Logic = LogicFactory.Instance.GetLogic();
_Logic.MoveDone += OnMoveDone;
}

public void OnMoveDone(int tableId, int? playerId, bool bBroadCast)
//fired by event in _Logic class
{
MoveDone (tableId, playerId, bBroadCast); //trigger event for
this wrapper class
}
So my query is, the above way of doing this is ok? Or is there some way i
canmake the MoveDone event in _Logic available to an external class directly
without the need for a seemingly redundant inner method. Something like:

public event MoveDoneEvent
{
get { return _Logic.MoveDone; }
}

Thanks
Jan 31 '07 #1
3 4832
On Jan 30, 4:10 pm, "Daniel" <nos...@pokercat.co.ukwrote:
Hi,

I have a scenario where a class is wrapped inside another but the inner
class triggers an event. I want that event to be accessed outside of the
wrapper. As a result i do code such as:

public Table()
{
_Logic = LogicFactory.Instance.GetLogic();
_Logic.MoveDone += OnMoveDone;
}

public void OnMoveDone(int tableId, int? playerId, bool bBroadCast)
//fired by event in _Logic class
{
MoveDone (tableId, playerId, bBroadCast); //trigger event for
this wrapper class
}

So my query is, the above way of doing this is ok? Or is there some way i
canmake the MoveDone event in _Logic available to an external class directly
without the need for a seemingly redundant inner method. Something like:

public event MoveDoneEvent
{
get { return _Logic.MoveDone; }

}
The way you've done it is a common idiom. The only other way I know of
is to expose the actual inner class, something like:

public Logic Logic
{
get { return this._logic; }
}

but one generally doesn't want clients messing directly with internal
objects, so this isn't commonly used.

Jan 31 '07 #2
Not only is this not "commonly used" I think that it's certainly an
architectural disaster waiting to happen.

My advice would be to NEVER expose a field in such a way and even if the
propagation of events in the manner described seems inelegant, as far as
performance goes the overhead is negligible.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

Bruce Wood wrote:
On Jan 30, 4:10 pm, "Daniel" <nos...@pokercat.co.ukwrote:
>Hi,

I have a scenario where a class is wrapped inside another but the inner
class triggers an event. I want that event to be accessed outside of the
wrapper. As a result i do code such as:

public Table()
{
_Logic = LogicFactory.Instance.GetLogic();
_Logic.MoveDone += OnMoveDone;
}

public void OnMoveDone(int tableId, int? playerId, bool bBroadCast)
//fired by event in _Logic class
{
MoveDone (tableId, playerId, bBroadCast); //trigger event for
this wrapper class
}

So my query is, the above way of doing this is ok? Or is there some way i
canmake the MoveDone event in _Logic available to an external class directly
without the need for a seemingly redundant inner method. Something like:

public event MoveDoneEvent
{
get { return _Logic.MoveDone; }

}

The way you've done it is a common idiom. The only other way I know of
is to expose the actual inner class, something like:

public Logic Logic
{
get { return this._logic; }
}

but one generally doesn't want clients messing directly with internal
objects, so this isn't commonly used.
Jan 31 '07 #3
Sorry, but I disagree.

There are several places in the Framework in which a class held as a
private instance is designed to be exposed through the wrapper class's
public properties. This usually happens when the class in question is
an aggregate.

ListViewItemCollection comes to mind: ListView doesn't just expose
methods / properties / events on its collection of list view items.
Instead, it exposes the collection via the Items property and lets you
work directly with it. The collection class, however, was designed
from the outset to protect its contents and let the caller do only
reasonable things with it.

It really all depends upon why your wrapper class is holding the inner
instance. If the outer class serves no other purpose than to wrap the
inner class, then I agree that it is better to link the inner class's
events / properties / etc through the outer class, thus making it
appear that the outer class implements them.

If, however, the outer class is doing a bunch of other things, and the
functions of the contained class are incidental to the outer class's
main function, it might be better to expose the contained class,
providing that it was carefully designed to not allow tampering from
sneaky client code. Again, ListViewItemCollection being an example in
the Framework of this approach.

On Jan 31, 1:46 pm, "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net>
wrote:
Not only is this not "commonly used" I think that it's certainly an
architectural disaster waiting to happen.

My advice would be to NEVER expose a field in such a way and even if the
propagation of events in the manner described seems inelegant, as far as
performance goes the overhead is negligible.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consultinghttp://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Trickshttp://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQhttp://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

Bruce Wood wrote:
On Jan 30, 4:10 pm, "Daniel" <nos...@pokercat.co.ukwrote:
Hi,
I have a scenario where a class is wrapped inside another but the inner
class triggers an event. I want that event to be accessed outside of the
wrapper. As a result i do code such as:
public Table()
{
_Logic = LogicFactory.Instance.GetLogic();
_Logic.MoveDone += OnMoveDone;
}
public void OnMoveDone(int tableId, int? playerId, bool bBroadCast)
//fired by event in _Logic class
{
MoveDone (tableId, playerId, bBroadCast); //trigger event for
this wrapper class
}
So my query is, the above way of doing this is ok? Or is there some way i
canmake the MoveDone event in _Logic available to an external class directly
without the need for a seemingly redundant inner method. Something like:
public event MoveDoneEvent
{
get { return _Logic.MoveDone; }
}
The way you've done it is a common idiom. The only other way I know of
is to expose the actual inner class, something like:
public Logic Logic
{
get { return this._logic; }
}
but one generally doesn't want clients messing directly with internal
objects, so this isn't commonly used.-
Feb 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jean-Gael GRICOURT | last post by:
I am trying to capture mouse events when entering and leaving a DIV layer. This test code works fine with IE 6.0 and Opera 7.21 but fails with Mozilla/Netscape. The strange thing is that the mouse...
2
by: Neil Makar | last post by:
I have the simplest of all possible web pages short of "Hellow world," and it doesn't friggin'work. I need someone to show me the secret handshake to get this working. I have an embedded object...
9
by: MLH | last post by:
open form in design view then allow it to be opened into form view from code. then set its visible propery to false then open into design view from code then set its visible propery to false...
5
by: JaD | last post by:
I am trying to understand events and delegates which is confusing me. Okay I have a user control that derives from a GroupBox called (MyPanel) I added this: public delegate void Collapsed();...
4
by: Curious Coder | last post by:
I have been tasked with a project that I do not think can be accomplished. Our company has an application that runs as an unmanaged ActiveX control on user desktops. It is designed to work with...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
6
by: Steve Hershoff | last post by:
Hi everyone, I've got a strange one here. There are two datagrids on my page, one nested within the other. I'll refer to them as the topmost and secondary datagrids. In the topmost...
8
by: MrNobody | last post by:
If I have a control like say a drop down list and I have some kind of onSelectItem change event, is there a way to temporarily suspend the event handling (without removing the event and then...
6
by: MLH | last post by:
I have a form, frmUSPSReturnReceipts, with a control named NotExpectingGreenTickets. The control's Exit event procedure follows: Private Sub NotExpectingGreenTickets_Exit(Cancel As Integer) If...
3
by: MikeK | last post by:
Ok, I've been noodling with this for several days now and I'm starting to go crazy. Does Apple's Safari browser support drag events on Textarea elements? The few specs and docs I've found seem to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.