none
OnCreated, OnLoaded, OnValidate Not Firing RRS feed

  • Question

  • Hi all,
     I"m building stuff by hand because I'm building a full O/RM that will also do Linq with validation etc.

    However, when I add void Onloaded(), void OnCreated() methods in my code, they never get fired when the objects are loaded or created as appropriate.

    What am I doing wrong?  How does one hook these up? I would have thought there would be an interface or something so that you could impliment it on the class and you're set.

    Thanks!
    Tuesday, March 25, 2008 4:25 PM

Answers

  • I figured out what the problem is.  Whoever did the coding, decided not to use an interface and because of that they're looking for specific implimentations. The result is that you can't put it in a base class (which is stupid) and you can't mark them as overridden. Heck, you can't even make them public or protected!

    This is why interfaces get used in the first place.

    MICROSOFT: Add basic inheritence support to Linq to SQL! You're driving me nuts defeating the #1 reason for an O/RM and why you'd do this!

    I should be able to put properties that map to fields in a base object that all others inherit from. I should be able to create virtual methods for the OnCreated, OnLoaded and OnValidate and then override them in any child class and reference the base like REAL Object Oriented Programming Works.

    You're doing the same *** with Object Entities as well.  Real programmers will get well outside the inane designer support really quickly. Give us the tools to be able to build rebust objects and do so before you release this stuff.  The number of people that your current version of Linq to SQL and Object Entities covers is about 20% of developers. The rest of us actually have to build real applications instead of "hello world" and need tools that work right.

    *HEAD SHAKE*
    Thursday, March 27, 2008 1:04 PM

All replies

  • OnCreated gets called in the code-generated constructor.  If you've written your own objects then you should add a call to OnCreated in the constructor.

     

    OnLoaded and OnValidate are called by the DataContext.  OnLoaded is called when you object is created via materialization.  OnValidate is called during SubmitChanges to let you to check for valid entity state before allowing the submit to go through.

     

     

     

    Wednesday, March 26, 2008 2:22 AM
    Moderator
  • Ok, so I'll add the OnCreated to the constructor, but the OnLoaded and OnValidated are not being called at all.   What am I missing?

     

    Also, I would have assumed that OnCreated would be called when the object was created for the first time (ie new), not every time that an object is created, and OnLoaded would be called when the object was loaded from the database.  This doesn't appear to be the way it works, because my default constructor gets called either way.

     

    OnValidated would be great, but it just never gets called.

     

    Suggestions as to why?

    Wednesday, March 26, 2008 1:14 PM
  • I don't know.  I hate to say it but I just tried it again with RTM and some random tables I just made up and then all get invoked as expected.  Possibly the method signatures are wrong.  Can you post the classes w/ the methods you've implemented (w/o the contents)?

     

    Wednesday, March 26, 2008 4:49 PM
    Moderator
  • I figured out what the problem is.  Whoever did the coding, decided not to use an interface and because of that they're looking for specific implimentations. The result is that you can't put it in a base class (which is stupid) and you can't mark them as overridden. Heck, you can't even make them public or protected!

    This is why interfaces get used in the first place.

    MICROSOFT: Add basic inheritence support to Linq to SQL! You're driving me nuts defeating the #1 reason for an O/RM and why you'd do this!

    I should be able to put properties that map to fields in a base object that all others inherit from. I should be able to create virtual methods for the OnCreated, OnLoaded and OnValidate and then override them in any child class and reference the base like REAL Object Oriented Programming Works.

    You're doing the same *** with Object Entities as well.  Real programmers will get well outside the inane designer support really quickly. Give us the tools to be able to build rebust objects and do so before you release this stuff.  The number of people that your current version of Linq to SQL and Object Entities covers is about 20% of developers. The rest of us actually have to build real applications instead of "hello world" and need tools that work right.

    *HEAD SHAKE*
    Thursday, March 27, 2008 1:04 PM
  • Greetings,

    I found this old post while I was searching google about the mentioned partial methods.

    I just want to know how the data context calls these methods? they are not an interface implementation nor a base class override, so how the data context knows of their existence? what remains is reflection, is this really the answer?
    Tuesday, July 22, 2008 9:13 PM