Parent property always null

Parent property always null

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


j055 posted on Wednesday, July 15, 2009

Hi

I'm using 3.6.2.0

Can you explain why the Parent properties in all my child objects are null? I have an editable root object which contains a managed property for an editable collection. The collection is lazy loaded and the DataPortal_Fetch of the collection calls MarkAsChild. There are grandchild objects too but not called using lazy loading so they should be marked as child automatically.

As far as I can tell, any call in any of the objects to the Parent propery always seems to be null. In particular I have found that when a call is made to this validation rule the target.Parent is null.

private static bool NoDuplicateExcludes(T target, RuleArgs e) where T : MyChild
{
var parent = (MyChildList) target.Parent;
if (parent != null)
{
foreach (var item in parent)
{
if (target.Exclude && !(ReferenceEquals(item, target)))
{
e.Description = "";
return false;
}
}
}
return true;
}

Is there something I need to do in the root object or the child collection which is lazy loaded? Any other ideas?

Many thanks
Andrew

j055 replied on Wednesday, July 15, 2009

Hi

Actually, I can see now that the parent isn't always null. I put the debugger in the PTWeb project and found that when AddNew is called

Role role = obj.AddNew();

the NoDuplicates rule is called and the Parent is null. Then when the DataMapper maps the values NoDuplicates is called again and this time it isn't null.

I guess this is by design but I'm struggling to see why. The parent obviously is there for the AddNewCore instance method to be called.

Where's the logic?

skagen00 replied on Wednesday, July 15, 2009

If the role is getting its rules checked before it actually gets added to the collection - and it tries to reference parent, it'll be null.

i.e. PersonAddress newAddress = PersonAddress.NewPersonAddress();
this.Add(newAddress);
return newAddress;

if you check rules in address looking for duplicates, parent will be null when DP_Create fires from the NewPersonAddress() sequence and it will check rules before the this.Add.

Not sure if that's what you're talking about but thought I'd mention it just in case.

j055 replied on Wednesday, July 15, 2009

Hi

Yes, you're right, that was the problem I was having. I guess it would be useful to know at what point in the ojects life cycle the Parent is available. Is it correct that it's not available after the object is deserialized? I can't quite work out when it's available using just a standard implementation. I'm not sure the book is very clear on the subject.

Thanks

Andrew

Copyright (c) Marimer LLC