Child collection not having parent set correctly

Child collection not having parent set correctly

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


DaveStorey posted on Monday, November 12, 2012

Hi,

When assigning values to child collections on a root object via a DataPortal fetch method call I have noticed that the Parent property of the collection object does not always get assigned correctly.

Digging a little deeper it would seem that using the SetProperty(propertyInfo, value) method call, regardless of being within a BypassPropertyChecks block or not, the CSLA code does not seem to assign a value to the Parent property. However, if the code is changed to a LoadProperty(propertyInfo, value) then the Parent property of the child collection is correctly set. My understanding of CSLA was that using the SetProperty method performed the same actions as the LoadProperty but with the added benefit of calling business rules and validation rules etc.

In the scenario I have a business rule on my collection that requires the Parent property be set  so I can execute my business rules, however as this is not getting set during the DataPortal fetch of the root object then my business rules are thrown exceptions due to null references.

Basically I guess my question is, why is the parent is not being set on the collection when SetProperty is used? If there is a valid reason how should I be assigning the value to ensure that my business rules execute correctly?

Regards,

Dave

JonnyBee replied on Monday, November 12, 2012

Which version of CSLA do you use?

Assuming CSLA 4.5:

1. If your child objects call BusinessRules.CheckRules in DataPortal_Fetch / DataPortal_Create then Parent will always be null as the child object is not yet added to the Parent.

2. How do you declare a business rule on the list?

DaveStorey replied on Monday, November 12, 2012

We are currently using v4.1

JonnyBee replied on Monday, November 12, 2012

So what you are saying is that the BusinessList does not have Parent set after SetProperty/LoadProperty is called in the parent?

DaveStorey replied on Tuesday, November 13, 2012

If I use SetProperty then the parent isn't getting set. If I use LoadProperty then the parent is getting set

DaveStorey replied on Tuesday, November 13, 2012

A little more background information:

We have an object called Client and an object called Role. The mapping between the two is managed by an object called ClientRole. There is also a collection object called ClientRoleCollection which is used to store all the ClientRole mappings for a given client. When we retrieve a User object from the database, during the Client DataPortal_Fetch method we also call the DataPortal.FetchChild<UserRoleCollection> and assign this value to the csla managed property Roles (of type ClientRoleCollection) on the client.

When we assign a new value into the ClientRoleCollection object we perform some business logic (not a business rule as I previously said) which checks the parent client objects AvailableRoles to ensure that the role being added is valid, and it is here that we have discovered the parent is null.

The following are snippets from the Client class:

public static Client GetClient(int clientId)

{

var client = DataPortal.Fetch<Client>(clientId);

client.CanPerform(AclOperations.GetObject, true);

return client;

}

 

private void DataPortal_Fetch(int clientId)

{

using (var ctx = DalManager.GetContext<IClientRepository>())

{

var dto = ctx.GetClient(clientId);

Child_Fetch(dto);

}

}

 

private void Child_Fetch(IClientDto dto)

{

using (BypassPropertyChecks)

{

Id = dto.ClientId;

Name = dto.Name;

Address1 = dto.Address1;

Address2 = dto.Address2;

City = dto.City;

County = dto.County;

PostCode = dto.PostCode;

}

// Load child objects

// ##################

Roles = DataPortal.FetchChild<ClientRoleCollection>(dto.ClientRoles);

 

}

 

This correctly sets the parent of the ClientRoleCollection when the value is assigned:

 

public static readonly PropertyInfo<ClientRoleCollection> RolesProperty = RegisterProperty<ClientRoleCollection>(c => c.Roles);

public IClientRoleCollection Roles

{

get { return GetProperty(RolesProperty); }

    set { LoadProperty(RolesProperty, value); }

}

 

This does not set the parent of the ClientRoleCollection when the value is assigned:

 

public static readonly PropertyInfo<ClientRoleCollection> RolesProperty = RegisterProperty<ClientRoleCollection>(c => c.Roles);

public IClientRoleCollection Roles

{

get { return GetProperty(RolesProperty); }

    set { SetProperty(RolesProperty, value); }

}

 

I hope this now make a bit more sense?

 

Regards,

Dave

 

 

Copyright (c) Marimer LLC