CslaAutoCloneOnUpdate and events

38 views
Skip to first unread message

Tiago

unread,
Nov 3, 2009, 7:48:59 PM11/3/09
to CslaGenerator
In future releases of Csla, the standard will be CslaAutoCloneOnUpdate
= true. This means that the event handlers we setup will be gone when
we update the object - the object is cloned but the non-serializable
part will be lost.

The correct way to handle it (according to another post (NonSerialized
() Custom Events by Bill Larman) is to used the OnDeserialized event
handler. In C# it goes like this:

/// <summary>
/// Set up event handlers so user code can respond to events
raised by generated code.
/// </summary>
protected override void Initialize()
{
base.Initialize();
AddHandlers();
}

/// <summary>
/// Set up event handlers so user code can respond to events
raised by generated code.
/// </summary>
/// <remarks>Event handler</remarks>
protected override void OnDeserialized
(System.Runtime.Serialization.StreamingContext context)
{
base.OnDeserialized(context);
AddHandlers();
}

private void AddHandlers()
{
//this.FetchPre += new EventHandler(OnFetchPre);
//this.FetchPost += new EventHandler(OnFetchPost);
this.UpdateStart += new EventHandler(OnUpdateStart);
//this.UpdatePre += new EventHandler(OnUpdatePre);
//this.UpdatePost += new EventHandler(OnUpdatePost);
//this.InsertStart += new EventHandler(OnInsertStart);
//this.InsertPre += new EventHandler(OnInsertPre);
//this.InsertPost += new EventHandler(OnInsertPost);
//this.DeletePre += new EventHandler(OnDeletePre);
//this.DeletePost += new EventHandler(OnDeletePost);
//this.FetchRead += new EventHandler(OnFetchRead);
//this.Create += new EventHandler(OnCreate);
}

#endregion

The Event handler region is the standard CslaGen.

The problem is some object stereotypes don't support OnDeserialized so
there is nothing to override. We can/must comment the method but how
can we be sure to re-setup the event handlers on update/clone?

On CSLA 3.0.5 the problems affects at ReadOnlyList and NameValueList.
On CSLA 3.7.1 besides the above lists, it also affects
BusinessListBase!

Andrés,

any suggestion?

Andrés Villanueva

unread,
Nov 4, 2009, 3:02:50 PM11/4/09
to cslage...@googlegroups.com
On CSLA >= 3.5 that wouldn't be a problem since we're switching to
partial methods (though there is no c# support for those in cslagen as
you already know).

Potentially, you could use the [OnDeserialized()] attribute in a
method (google that a bit). I haven't tested that, and I don't know if
it would have any side effects.
All I know that the binary formatter will look for a method that's
decorated with that attribute and has the correct signature.
By side effects, I'm only talking about a possible case where
readonlylist (just to name one) already has that attribute in a base
class, but the method is not overridable (again, I don't know, I don't
have the source code handy and I don't remember). I'm not sure what
would happen in that scenario, but other that, you should be fine with
such method as a replacement for the OnDeserialized override.
--
Andrés

Tiago

unread,
Nov 12, 2009, 5:38:08 PM11/12/09
to CslaGenerator
I asked the question in CSLA.NET forum and got an answer from Rocky.
The resulting code goes like this

#region Initialize

/// <summary>
/// Set up event handlers so user code can respond to events
raised by generated code.
/// </summary>
protected override void Initialize()
{
base.Initialize();
AddHandlers();
}

/// <summary>
/// Set up event handlers so user code can respond to events
raised by generated code.
/// </summary>
/// <remarks>Event handler</remarks>
protected virtual void OnDeserialized
(System.Runtime.Serialization.StreamingContext context)
{
// missing base OnDeserialized on this stereotype
AddHandlers();
}

[System.Runtime.Serialization.OnDeserialized]
private void OnDeserializedHandler
(System.Runtime.Serialization.StreamingContext context)
{
OnDeserialized(context);
}

private void AddHandlers()
{
//this.FetchPre += new EventHandler(OnFetchPre);
//this.FetchPost += new EventHandler(OnFetchPost);
}

#endregion

Andrés Villanueva

unread,
Nov 12, 2009, 5:54:28 PM11/12/09
to cslage...@googlegroups.com
Yep, That's about it.
> --
> http://groups.google.com/group/CslaGenerator
> Post to: CslaGe...@googlegroups.com
> Unsuscribe: CslaGenerato...@googlegroups.com
> Join us on #CslaGenerator @ irc.freenode.net



--
Andrés
Reply all
Reply to author
Forward
0 new messages