Minor issue with unused partial methods

Nov 12, 2009 at 7:32 AM

This is a small thing, but in the generated code from the templates, there's a section at the top for each table that looks like this:

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(ChangeAction action);
partial void OnCreated(); 
#endregion
 

The OnCreated() partial method is fine, but the OnLoaded() and OnValidate() partials aren't used anywhere are far as I can see. Are they there for a reason or just accidentally overlooked junk?

Nov 12, 2009 at 10:00 AM

They are there if you want to hook into these events for the entity. It is a cleaner way of specifying the default behavior of events, as you do not have to inherit and override.

I have used the OnValidate for custom validation, like Scot Gu does with NerdDinner.
http://www.nerddinner.com/

They are partial methods. Which means that if they are not implemented the compiler will remove it at compile time, so there is no performance penalty.
http://blogs.msdn.com/wesdyer/archive/2007/05/31/more-on-partial-methods.aspx

Nov 13, 2009 at 6:20 AM

I think that partial methods are perfect for extending generated code and they work great for OnCreated() because that's actually called when an entity is created. The thing with OnValidate() and OnLoaded() is that nothing in the generated code ever calls them as far as I can tell, so there's nothing to extend.

Mar 3, 2010 at 6:52 AM

I've never really understood how this all works, but if you implement these partial methods, I assure they will get called by the L2S infrastructure.  I still haven't figured out how the infrastructure is able to call what should be a non-public method though.  Since these are POCOs the full source code is there for inspection so I just don't understand how it is able to call these methods.  Is anyone able to explain this?