C# 2.1 Adding events to objects

C# 2.1 Adding events to objects

Old forum URL: forums.lhotka.net/forums/t/3510.aspx


JonM posted on Tuesday, September 11, 2007

I remember the in the CSLA.net 1.0 events were not possible.  Has the situation gotten any better with 2.0?  I've found a few posts about VB but have not been able to make much sense out of them.  I need to add an event to a c# business object.  I'm having the age old problem where the dataportal now wants to serialize the event target (my winforms app!).  Is there anyway around this in CSLA.net 2.1 in c#?

RockfordLhotka replied on Tuesday, September 11, 2007

Yes, look at the implementation of the Saved event in BusinessBase and copy that. The technique is discussed in both Expert C# Business Objects (chapter 3 I think) and the CSLA .NET Version 2.1 Handbook (regarding the Saved event).

clertem replied on Thursday, September 13, 2007

A friend of mine asked me "Why events in the business layer", and I was very bad in replying why.

Can you give me a fool-proof answer please ?

Thanks mate

RockfordLhotka replied on Thursday, September 13, 2007

No, I can't. I generally recommend against relying on custom events in the business layer - especially to cross logical layer boundaries.

Microsoft has defined a very clear mechanism for communication between the UI and business layers: data binding. My recommendation is to use data binding, and to use the interfaces/events defined by Microsoft for data binding. This gives you the best decoupling, while still leveraging all the pre-built functionality in Web Forms, Windows Forms and WFP.

If you are using events for communication within the business layer, then that's OK. And if that makes sense within a given object model, then go for it. But let's face it, most object models don't need events beyond those provided by the data binding events.

jhw replied on Friday, September 14, 2007

But if we do want to handle custom events in the ui, can we now do it in vb, or is it still limited to c# as was the problem back in version 1.

 

RockfordLhotka replied on Friday, September 14, 2007

The event declaration technique in C# and VB are the same in .NET 2.0. Again, look at the Saved event for guidance.

xal replied on Friday, September 14, 2007

Yes, only not in the way we're used to. Check how ExtendedBindingList.vb inside csla.core for an example of how you should do it.


Andrés

Copyright (c) Marimer LLC