This project has moved and is read-only. For the latest updates, please go here.

Creating children collections

Mar 31, 2011 at 8:25 PM

I am not sure if I am encountering a bug in CGF or if I just don't know what I am doing (highly likely).  I tried looking through the csla samples but the all seem to be more simplistic than this.

I have an object ParentCollection that contains Parent Objects.  The Parent Object contains a child ChildCollection of Child Objects.

I am trying to do this:

ParentCollection PC = ParentCollection.NewParentCollection();

Parent P = Parent.NewParent();

ChildCollection CC = P.ChildCollection.AddNew();

//Add stuff to the child collection etc.

I saw this use of the AddNew function in an example somewhere so I have no idea if I am going about this correctly.  The AddNew function wants to return a Child Object not a ChildCollection.  I have checked over my CGF and they seem right.  Where should I be looking to find this problem.

I have also tried the code below but get an "object referance not set to an instance of an object" error

Child C = Parent.ChildCollection.AddNew();

I also have tried this code below but get an "object referance not set to an instance of an object" error because Parent.ChildCollection is null to start with.

Parent.ChildCollection.Add(Child);

Is there some code that is not generated by CGF that I need to write to make Parent.ChildCollection a new but empty ChildCollection?

I'm thrashing and need a push in the right direction.

Mar 31, 2011 at 9:09 PM

After some more internet searching I found the codesmith petshop example.  In their unit tests for the petshop they do exactly what I am trying.

Product product = category.Products.AddNew();

Assert.IsTrue(category.Products.Count == 1);

It looks like when I call P.ChildCollection.AddNew(); it is not creating the child collection.  I'm still looking for why or where to look. 

Apr 1, 2011 at 1:12 AM

Look for DataPortal_Create in your Parent class

You should have some code generated which does a LoadProperty to load a new empty ChildCollection into your property on the parent

Something like

protected override void DataPortal_Create()
{
  LoadProperty(ChildCollectionProperty, ChildCollection.NewChildCollection());

  // etc etc....

  BusinessRules.CheckRules();
}

Make sure this is there, if it is, put a breakpoint on and make sure DataPortal_Create is invoked. By the way, what's your setup (CSLA version/application type etc)

Apr 1, 2011 at 2:10 PM

I do not have this function on my parent.  What triggers this to be created in CGF?  What setting should I look at since this is missing?

Thanks for you quick response!

Mark

Apr 1, 2011 at 2:16 PM

In an effort to try something I started checking and unchecking stuff to see if I could get DataPortal_Create to generate.

In the Project Properties I had "Use child DataPortal methods" checked.  When I uncheck this and generate I have the DataPortal_Create generated.  Is this supposed to be checked?

Thanks, Mark

Apr 1, 2011 at 2:26 PM

Okay wierd. 

I tried running the generated code with child DataPortal methods uncheck but there were a lot of errors.  I decided to check the checkbox regenerate and then run just to see what happens.  I was expecting everything to be generated the same as before and not work but surprise!!  The code is different this time with child DataPortal check than it was before with it checked and now my child collection is created!!  Not sure what I did but I am glad it seems to be working!  I will have to do some more testing to make sure.