Events and aggregates

E

earth

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.
 
M

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

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
 
M

Marc Gravell

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
 
E

earth

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.
 
M

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

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
 

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