473,378 Members | 1,457 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,378 software developers and data experts.

Events and aggregates

If I have a class call it ClassA which is an aggregate of ClassB.

ClassA delegates to ClassB (I mean that in the English sense of the
word not in the C# reserved word sense). So for instance
ClassA.MyMethod calls ClassB.MyMehtod.

ClassA also has an event but the event is actually raised by ClassB.
The event can be called FooRaised.

The handler of the event will be on a third class ClientClass and
ClientClass will call ClassA.FooRaised += FooRaisedHandler.

Now,

I could have and event handler in ClassA that is hooked to the exposed
event of ClassB. When ClassB raises the event the ClassA handler will
raise the event on ClassA and the ClientClass.FooRaisedHandler will be
called.

However I don't want a handler in ClassA to be hooked to the event on
ClassB and effectively pass the event on. I want the handler in
ClientClass to be passed through to the event in ClassB so that when
ClassB raised the event the handler on ClientClass is called directly.

I could do this in other languages because events have accessor
methods where the handler is passed. In the set accessor I would
assign the handler to the aggregated class. In C# it does not appear
that events have accessor methods. This seems like a great big hole.

Is there a way to pass though the handler as I described?

I have considered a method on ClassA that accepts the event handler
type and will assign it to the event in ClassB but it is not really
the right way to expose an event.
Jun 27 '08 #1
8 1172
Yes; you can use explicit event implementation to do exactly this:

public class ClassB {
public event EventHandler SomeEvent;
}
public class Class A {
private readonly ClassB b = new ClassB();

public event EventHandler SomeEvent {
add {b.SomeEvent += value;}
remove {b.SomeEvent -= value;}
}
}

The only problem with this approach is that callers will see the "b"
instanc as the "sender", not the instance of ClassA to which they
subscribed. This can be confusing, and can prevent some binding-sources
(in particular list-based) from correctly identifying the origin of the
event.

Marc
Jun 27 '08 #2
On Jun 2, 12:11 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Yes; you can use explicit event implementation to do exactly this:

public class ClassB {
public event EventHandler SomeEvent;}

public class Class A {
private readonly ClassB b = new ClassB();

public event EventHandler SomeEvent {
add {b.SomeEvent += value;}
remove {b.SomeEvent -= value;}
}

}

The only problem with this approach is that callers will see the "b"
instanc as the "sender", not the instance of ClassA to which they
subscribed. This can be confusing, and can prevent some binding-sources
(in particular list-based) from correctly identifying the origin of the
event.
And of course one answer to that is to make ClassA subscribe (once) to
ClassB's event, and then call its own list of handlers when ClassB's
event is raised, merely changing the sender.

Jon
Jun 27 '08 #3
Yes, but the OP covered that already (at length) stating:
However I don't want a handler in ClassA to be hooked to the event
on ClassB and effectively pass the event on.
Marc
Jun 27 '08 #4
On Jun 2, 12:11 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Yes; you can use explicit event implementation to do exactly this:

public class ClassB {
public event EventHandler SomeEvent;}

public class Class A {
private readonly ClassB b = new ClassB();

public event EventHandler SomeEvent {
add {b.SomeEvent += value;}
remove {b.SomeEvent -= value;}
}

}

The only problem with this approach is that callers will see the "b"
instanc as the "sender", not the instance of ClassA to which they
subscribed. This can be confusing, and can prevent some binding-sources
(in particular list-based) from correctly identifying the origin of the
event.

Marc
Thats great. I could not imagine that something like this would be
missed. When it comes to the sender of the event, in this case class
b has a reference to class a so I can put that in the sender
parameter. Overall I think it is ok to do that because it properly
encapsulates class b.
Jun 27 '08 #5
One other thought - if "b" has a reference to "a", it could equally
simply call an internal OnSomeEvent method on "a", and let "a" host the
event. Either way similar end result.

Marc
Jun 27 '08 #6
On Jun 2, 1:32 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Yes, but the OP covered that already (at length) stating:
Well if you're going to be picky and expect me to start actually
reading the question, I might as well stop posting ;)

Jon
Jun 27 '08 #7
On Mon, 02 Jun 2008 05:32:51 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
Yes, but the OP covered that already (at length) stating:
However I don't want a handler in ClassA to be hooked to the event
on ClassB and effectively pass the event on.
He did write that. But it's not very clear _why_ he wrote that. It would
be helpful if the OP could explain why it is he doesn't just want ClassA
to act as a proxy. That's basically what the delegation of the rest of
the things ClassB does would do anyway. What's so bad about it just
because it's an event being delegated?

Pete
Jun 27 '08 #8
On Mon, 02 Jun 2008 06:08:57 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
One other thought - if "b" has a reference to "a", it could equally
simply call an internal OnSomeEvent method on "a", and let "a" host the
event. Either way similar end result.
Indeed. I think it's a little odd that ClassB "knows" about ClassA anyway
(it makes the whole delegation thing a little awkward), but IMHO given
that it does I'd say that having ClassB just tell ClassA to raise the
event (as Marc suggests) is a better approach than having ClassB
impersonate ClassA.

Pete
Jun 27 '08 #9

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

Similar topics

3
by: Sasha | last post by:
Hi everyone, Here is my problem: I have the following classes: - DataNode - this class is designed to hold some data and will be contained in a tree like data structure DataTree. When...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
12
by: jao | last post by:
I have an application with a table that tracks objects with a "size" attribute. What we want to do is to periodically report on the number of these objects and the sum of the object sizes. The...
1
by: Esteban Kemp | last post by:
This is the Problem: I'm building a Large DataMart with a big table and I want to improve the performace using aggregates I mean a set of table that store some specific aggregacion of the main...
2
by: John Smith | last post by:
Hi, I have a question regarding the initialisation of aggregates: The C (99) standard states: section 6.7.8, paragraph 21 states: If there are fewer initializers in a brace-enclosed list than...
14
by: xoozlez | last post by:
Hi there, I have a registration form where I like to filter out the past events of 2007. This is the code I am using : strSQL = "SELECT EventID, EventName, EventDateBegin, EventDateEnd,...
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.