473,216 Members | 2,240 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,216 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 1167
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...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.