Traverse related entities or related entity collection from an entity instance

Posts   
 
    
evdinursan avatar
evdinursan
User
Posts: 25
Joined: 22-Jul-2005
# Posted on: 31-May-2016 12:51:26   

Hi,

I'm using LLBLGen 4.2 (Release December 11th, 2015) and what I'm trying to do is pretty simple - using DI architecture (Unity), I'm injecting a simple class instance in all my business classes requests in order to be able to set the user that is creating / updating data - this is working and I don't have any issue so far.

So, the flow is like this:

a) create the instance of a model class in UI with data taken from the UI b) UI calls BL (Business Layer) in order to save that model class (that is transformed into an entity) - here, using DI, UI also injects into BL the info regarding the current authenticated user c) the BL saves the entity using LLBL.

Perfect!

My issue is when I'm sending from UI to BL a more complex structure (for example invoice with collection of invoice details) because in BL the saving process is just one line (thanks to LLBL sunglasses and recursive save) but at this point I have no idea how can I set the user info on the "children" entities (in my case - the invoice details entities). I'm looking for a way of traversing all "children" entities and entity collections to see if there is something and automatically set the user info.

Thank you in advance for your guidance. Evdin

evdinursan avatar
evdinursan
User
Posts: 25
Joined: 22-Jul-2005
# Posted on: 31-May-2016 13:39:19   

I think I found a solution. Can you please tell me what do you think?


ObjectGraphUtils objectGraphUtils = new ObjectGraphUtils();
                        List<IEntity2> flatList = objectGraphUtils.ProduceTopologyOrderedList<IEntity2>(entity);
                        foreach (IEntity2 relatedEntity in flatList)
                        {
                            if (relatedEntity.IsNew)
                            {
                                relatedEntity.Fields["CreatedBy"].CurrentValue = this._ownerInfo.CreatedBy;
                                relatedEntity.Fields["CreatedOn"].CurrentValue = this._ownerInfo.CreatedOn;
                            }
                            else
                            {
                                relatedEntity.Fields["ModifiedBy"].CurrentValue = this._ownerInfo.CreatedBy;
                                relatedEntity.Fields["ModifiedOn"].CurrentValue = this._ownerInfo.CreatedOn;
                            }
                        }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Jun-2016 08:37:58   

Yes, that is the way to go there. Just a recommendation: it's preferable to use entity.SetNewFieldValue(fieldName, value) to set a value as it respects the change cycle (auditing, validation, update IsChanged and IsDirty flags, etc)

ObjectGraphUtils objectGraphUtils = new ObjectGraphUtils();
                        List<IEntity2> flatList = objectGraphUtils.ProduceTopologyOrderedList<IEntity2>(entity);
                        foreach (IEntity2 relatedEntity in flatList)
                        {
                            if (relatedEntity.IsNew)
                            {
                                relatedEntity.SetNewFieldValue("CreatedBy", this._ownerInfo.CreatedBy);
                                ...
                            
                        }
David Elizondo | LLBLGen Support Team
evdinursan avatar
evdinursan
User
Posts: 25
Joined: 22-Jul-2005
# Posted on: 02-Jun-2016 10:53:17   

Thank you very much. It works great sunglasses