BusinessBase child objects fail to instantiate

BusinessBase child objects fail to instantiate

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


BBM posted on Friday, May 05, 2006

I have an application that I am trying to switch from the C# version of CSLA to the VB version (1.52)  in preparation for re-writing the app in VB.  I was concerned about whether the framework would work identically, but so far, except for some minor re-naming that I had to do in th C# source, it seems to work fine,  EXCEPT...

I have a very simple BusinessCollectionBase object that is an attribute of one of my main entity objects.  It contains "child" BusinessBase objects.  The code in the BusinessCollectionBase object creates a DataReader and interates over it.  On each iteration it calls the "get" static method in the child class.  The child "get" method looks like this...

public static DesAttribute GetDesAttribute(IDataReader dr)

{

      try

         {

            DesAttribute da = new DesAttribute();

            da.Fetch(dr);

            da.MarkAsChild();

            da.AddBusinessRules();

            return da;

         }

   catch (System.Exception ex)

         {

                  return null;

         }

}

Nothing fancy here, but the program throws an exception as it tries to execute the declaration (private DesAttribute()) of the constructor of the child object and it gets caught in my catch block above.  None of the statements in the constructor run.  The message of the exception says that

"Object reference not set to an instance of an object."

Which I presume is true because da is never instantiated?

One thing you should know is that I am running a "remote" DataPortal (under IIS) on the same machine.  Also, the same C# code runs fine with the C# CSLA Framework on another computer.

I'm stumped (admittedly not that hard to do).

Any hints or debugging suggestions are welcome.

BBM

RockfordLhotka replied on Friday, May 05, 2006

I suggest you debug to find out if da is indeed null, or if it is the Fetch() method that's causing the exception.
 
Rocky


From: BBM [mailto:cslanet@lhotka.net]
Sent: Friday, May 05, 2006 4:25 PM
To: rocky@lhotka.net
Subject: [CSLA .NET] BusinessBase child objects fail to instantiate

I have an application that I am trying to switch from the C# version of CSLA to the VB version (1.52)  in preparation for re-writing the app in VB.  I was concerned about whether the framework would work identically, but so far, except for some minor re-naming that I had to do in th C# source, it seems to work fine,  EXCEPT...

I have a very simple BusinessCollectionBase object that is an attribute of one of my main entity objects.  It contains "child" BusinessBase objects.  The code in the BusinessCollectionBase object creates a DataReader and interates over it.  On each iteration it calls the "get" static method in the child class.  The child "get" method looks like this...

public static DesAttribute GetDesAttribute(IDataReader dr)

{

      try

         {

            DesAttribute da = new DesAttribute();

            da.Fetch(dr);

            da.MarkAsChild();

            da.AddBusinessRules();

            return da;

         }

   catch (System.Exception ex)

         {

                  return null;

         }

}

Nothing fancy here, but the program throws an exception as it tries to execute the declaration (private DesAttribute()) of the constructor of the child object and it gets caught in my catch block above.  None of the statements in the constructor run.  The message of the exception says that

"Object reference not set to an instance of an object."

Which I presume is true because da is never instantiated?

One thing you should know is that I am running a "remote" DataPortal (under IIS) on the same machine.  Also, the same C# code runs fine with the C# CSLA Framework on another computer.

I'm stumped (admittedly not that hard to do).

Any hints or debugging suggestions are welcome.

BBM




BBM replied on Friday, May 05, 2006

Hi Rocky,

Thanks for your help.  I tried your suggestions.  da is indeed null.  Fetch() never gets called.

I rearranged the code a little so that I could examine da from inside the catch block.

public static DesAttribute GetDesAttribute(IDataReader dr)

  {

      DesAttribute da;

      try

      {

         da = new DesAttribute();

      }

      catch (System.Exception ex)

      {

            return null;

      }

      da.Fetch(dr);

      da.MarkAsChild();

      da.AddBusinessRules();

      return da;

}

When I examined the stack trace of the exception, it showed that the exception was actually thrown in AddBusinessRules() (I have an override version in DesAttribute).  I added CSLA to my Solution, and found that the BusinessBase Constructor is getting called on each instantiation, even though I don't explicitly call it, and it's calling AddBusinessRules.  AddBusinessRules fails because an object reference it needs hasn't been set yet (won't be until Fetch() is complete).

My constructor looks like this...

private DesAttribute()

{

      createDate = DateTime.Now;

      createUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

      lastDate = DateTime.Now;

      lastUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

}

The class declaration looks like this...

[Serializable()]

public class DesAttribute : BusinessBase

Any idea why the BusinessBase constructor is getting called?   It happens right after the declaration of the DesAttribute constructor before any code in the constructor runs.  It's just as if I called MyBase.New() (the C# equivalent of course) as the first line in the Constructor.

Can you shed some light on this?  I think I'll search the old message board, and also step through my "pure" C# version to see what happens. 

BBM

RockfordLhotka replied on Friday, May 05, 2006

Constructors are always called all the way up the inheritance chain. Actually all the way down, since the deepest one runs first, then the next, all the way out to yours. That's just the way constructors work.
 
Perhaps the C# framework is missing a call to AddBusinessRules in its ctor? The call should be there, and should be triggered when the object is created. Rules shouldn't be _triggered_ there - just configured. In other words, your AddBusinessRules method is probably calling CheckRules - which it should not.
 
Rocky


From: BBM [mailto:cslanet@lhotka.net]
Sent: Friday, May 05, 2006 10:27 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] RE: BusinessBase child objects fail to instantiate

Hi Rocky,

Thanks for your help.  I tried your suggestions.  da is indeed null.  Fetch() never gets called.

I rearranged the code a little so that I could examine da from inside the catch block.

public static DesAttribute GetDesAttribute(IDataReader dr)

  {

      DesAttribute da;

      try

      {

         da = new DesAttribute();

      }

      catch (System.Exception ex)

      {

            return null;

      }

      da.Fetch(dr);

      da.MarkAsChild();

      da.AddBusinessRules();

      return da;

}

When I examined the stack trace of the exception, it showed that the exception was actually thrown in AddBusinessRules() (I have an override version in DesAttribute).  I added CSLA to my Solution, and found that the BusinessBase Constructor is getting called on each instantiation, even though I don't explicitly call it, and it's calling AddBusinessRules.  AddBusinessRules fails because an object reference it needs hasn't been set yet (won't be until Fetch() is complete).

My constructor looks like this...

private DesAttribute()

{

      createDate = DateTime.Now;

      createUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

      lastDate = DateTime.Now;

      lastUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

}

The class declaration looks like this...

[Serializable()]

public class DesAttribute : BusinessBase

Any idea why the BusinessBase constructor is getting called?   It happens right after the declaration of the DesAttribute constructor before any code in the constructor runs.  It's just as if I called MyBase.New() (the C# equivalent of course) as the first line in the Constructor.

Can you shed some light on this?  I think I'll search the old message board, and also step through my "pure" C# version to see what happens. 

BBM




BBM replied on Saturday, May 06, 2006

Thanks for your help.  I am clearly mistaken on how the base constructor gets called and apologize for wasting your time on this.

The constructor in the C# framework code (ver 1.4 I think) is missing the AddBusinessRules() call (in fact I don't see a Constructor in BusinessBase or it's parent classes).

Good luck with your new book(s) - I got mine yesterday.

Best Regards.

BBM

 

  

Copyright (c) Marimer LLC