StrataFrame Forum

Access childBusinessObject

http://forum.strataframe.net/Topic9269.aspx

By Chan - 6/2/2007

Hi,

How could I access childbusinessobject from parentbusinessobject? I checked SF source code and found that is a private variable _ChildBusinessObjects. However it is not exposed. Any other way to access it?

Thank you

By StrataFrame Team - 6/4/2007

No, but I will add a property to expose that collection for the next release.  In the meantime, you can add the property and rebuild the source code if you need it.
By Chan - 6/4/2007

Hi,

May I know the property name?

Thank you

By StrataFrame Team - 6/4/2007

We'll call it the same as the private field without the _, so probably ChildBusinessObjects.
By Randy Jean - 6/4/2007

I have a related question... How do I access a parent business object from the child? In the instance of the child? I want to conditionally run some custom currentrowvalidation in the child business object based on a value in the parent.



Thanks.
By Peter Jones - 6/4/2007

Hi,

I think that would be simply:

me.ParentBusinessObject....

Cheers, Peter

By Ben Hayat - 6/4/2007

Randy Jean (06/04/2007)
I have a related question... How do I access a parent business object from the child? In the instance of the child? I want to conditionally run some custom currentrowvalidation in the child business object based on a value in the parent.



Thanks.




How about:

ParentFormName.MyBO



Should give you access to it!
By Trent L. Taylor - 6/4/2007

Either of the samples given to you from Peter or Ben Hayat would work.  Since a child BO has to have the parent instance set, you can just access that property directly. 

MyChildBO.ParentBusinessObject
By Randy Jean - 6/5/2007

but... I need to access a value on the business object and at design time it's not there so I can't compile. I'm from the loosely typed dynamic world of VFP where this type of thing isn't an issue so I'm still confused with where/how to reference things at design time.



So, for instance, I want to do the following in my child CheckRulesOnCurrentRow:







If Me.ParentBusinessObject.InstrumentName = "Atlas" Then ' <- Compile time error due to ParentBusinessObject doesn't exist at design time

If Not CheckOnePortActive(Me.CurrentRowIndex) then

Me.AddBrokenRule(PortSetupBoFieldNames.Activate,"Only one port can be active for this instrument.")

End If

End If

By Randy Jean - 6/5/2007

I tried this and it compiles but at runtime oParentBiz is NULL:





Dim oParentBiz As InstrumentSetupBO

oParentBiz = CType(Me.ParentBusinessObject, InstrumentSetupBO)

If oParentBiz.InstrumentName = "Atlas" Then

If Not CheckOnePortActive(Me.CurrentRowIndex) Then

Me.AddBrokenRule(PortSetupBOFieldNames.Activate, "Can only have 1 active port on Atlas.")

End If

End If

By Trent L. Taylor - 6/5/2007

Randy,

You are kindof fighting against the grain of .NET and is more related to that versus SF.  First of all, I too understand what is was to be weak-typed as I too came from VFP.  Once you understand and begin to work within the strong-typed environment you will look back with utter disgust the weak-typed environment Smile  ... I promise.  But you are right, there are a few things to learn. 

First, you are trying to use an operator (=) to test an object reference.  This doesn't work in .NET.  You need to respect the typed environment and remember that a string is an object too.  If you look at it's derived type it is actually an object.  So you will need to test on it like this:

If Me.ParentBusinessObject.InstrumentName.Equals("atlas") Then
End If
By Randy Jean - 6/5/2007

This still doesn't solve my problem and more importantly does not work. I'm putting this in my child CheckRulesOnCurrentRow and ParentBusinessObject does not exist at design time so I cannot even compile this. It doesn't work putting this in the instance either.



If I try your code I get:



'InstrumentName' is not a member of 'Microfour.Strataframe.Business.BusinessLayer'



this is at design time so I cannot even compile.
By Randy Jean - 6/5/2007

Trent L. Taylor (06/05/2007)
Randy,



You are kindof fighting against the grain of .NET and is more related to that versus SF.







I agree, but since I'm still learning both figured this is a better place to get real-world answers vs. theory.



b]Trent L. Taylor (06/05/2007)[/b]


Once you understand and begin to work within the strong-typed environment you will look back with utter disgust the weak-typed environment Smile ... I promise.




I don't know, all I can do is keep trying but I would say I'm still not even 10% as productive in .NET as I was in VFP. And it's not that I'm not OOP saavy - I use an nTier OOP framework for years. What I'm trying to do is as simple as:







IF TYPE("This.oParentBizObj") = T_OBJECT

With This.oParentBizObj

If .GetField("INSTRUMENTNAME") = "Atlas"

IF Not This.CallMyValidationCode()

*** update error collection...

endif

Endif

Endwith

ENDIF







Now, even though I don't get intellisense at design time I KNOW this will work or at least not blow up if oParentBizObj is not an object. .NET has to use fancy stuff like "Reflection" to pull of something "dynamic" like this. Not to start a debate, but I consider myself pretty smart and I've been nothing but frustrated whenever I try to do what seems should be straightforward and logical. Not to say I don't like some things about .NET, especially the improved forms and controls and how much easier it is to create a fancier screen.... but my bread and butter is in the business logic and data processing, not fancy screens. So it seems like not a very fair trade-off for me (yet)



So, I'm feeling really dense right now because I still have not "got" how to check a value in the parent bizobj from the child. I have a working parent/child screen so I know the stuff is set up correctly, I just can't figure out where/how to do what should be a simple check of a value in the parent from the child.



Also, I just pulled up the Strataframe source to see if I could "learn" from that (that's how I learned how best to use the VFP framework over the years) - I can't even pull up the Business Layer class in the designer. And when I did a rebuild all my reference to Microfour.Strataframe.Application is gone! And I still could not see how to reference the parent business object - What is _ParentBusinessObject?
By Greg McGuffey - 6/5/2007

Randy,



I can't answer your theoretical questions, but I might offer some insight into how to do what you propose in your code. The thing to recognize is that based on the code below, you know the type of the parent business object and the field you want to check, so strong typing becomes your friend BigGrin



Here is my suggestion at some .NET code. A couple of notes. First, I just typed this in, so there may be some spelling errors/syntax errors. I just wanted to give you an idea of how to do what you are trying to do. Second, Me is the object reference to the instance of a type in VB ('this' is used in C#). I've written this as if it was happening on a form, with a child bo named myChildBO. I've called the parent BO InsturmentBO. Any or all of that could be wrong...again I hope this gives you some idea of how it might be done in .NET.





'-- Validate that a parent BO is set

If Me.myChildBO.ParentBusinessObject IsNot Nothing then

'-- Cast parent BO to its actual type

Dim myParentBO As InsturmentBO = DirectCast(Me.myChildBO.ParentBusinessObject,InsturmentBO)



With myParentBO

'-- InsturmentName is now a strongly typed property, you make changes to your InsturmentBO

' and this will not compile...no hidden bugs

If .InsturmentName = "Atlas" Then

'-- Call validation code

End If

End With

End If





Finally, this works if you know what the parent BO type is and what the field name is. If you don't, then you could use this code instead:





'-- Validate that a parent BO is set

If Me.myChildBO.ParentBusinessObject IsNot Nothing then

'-- Can't cast the BO, but all BOs are wrapping an ADO.NET DataTable, which we can use

Dim myParentDataTable As System.Data.DataTable = Me.myChildBO.ParentBusinessObject.CurrentDataTable



'-- You can access any column via the rows.item property of a datatable.

' fieldName is a variable that is filled with the name of the column to check and

' validData is a variable to check it against. This likely would only work if you

' knew the data type of the validData, in this case I'm using a string.

If CType(myParentDataTable.Rows.Item(fieldName),String) = validData Then

'-- Call validation code

End If

End If





I hope this helps. I was pulling my hair out (what little there was to start with) when I started .net and SF about 7 months ago. Hang in there, there is light at the end of the tunnel (and it's probably not a train Blink ).
By Randy Jean - 6/5/2007

Thanks Greg,

At least this code doesn't blow up and all seems to look good at design time. (and it's only a little more complicated than my VFP example BigGrin) However, my validation code is not running still as I suspected it would not because at runtime Me.ParentBusinessObject is in fact Nothing (or NULL)



So, maybe this is a different issue altogether. Any ideas why ParentBusinessObject would be Nothing if I have my Parent/Child properties set properly?
By Randy Jean - 6/5/2007

Figured it out. My relationship was invalid after renaming the parent class quite some time ago so I just had to reset the ParentRelationship. This is the problem with not being able to focus on one project at a time. So, it's all good now. Thanks for all the help.
By Randy Jean - 6/5/2007

Ben Chase (06/04/2007)
No, but I will add a property to expose that collection for the next release. In the meantime, you can add the property and rebuild the source code if you need it.




I'm feeling pretty confident now... I needed this too and actually got it to work no problem.



Question: in this case I had 1 child so I knew it was just Me.ChildBusinessObjects(0) to get my reference. I'm assuming I can loop through the collection checking for the name of the childbusiness object I need a reference to. Can someone post an example? I'm assuming it's a for loop of some type.



Thanks,

Randy
By StrataFrame Team - 6/6/2007

If all of the children are of different types, then you will want to use a For Each loop to get the one you want like this:

For Each child As BusinessLayer In Me.ChildBusinessObjects
    '-- Test the type of the child
    If child.GetType() Is GetType(MyChildType) Then
        '-- Process the child
    End If
Next

By Greg McGuffey - 6/6/2007

Glad you are getting it figured out. BigGrin