Wednesday, January 11, 2006
CSLA.NET Switching root to child object and vice versa
I’ve managed to create my own switchable mechanism in CSLA .NET for editable object. I did tried to implement the original switchable editable object as presented by the author, but it confuses me a lot. As far as I understand to make the editable object switchable from root to child is by putting MarkAsChild() in the constructor of the object. When an editable child object is implemented calling Save() method it would throw an exception because it can’t be save directly. However, if we can manage to change the IsChild property from true to false then there will be no problem. But IsChild is a read only property. I know this is a hack version against the original implementation and I guess a lot of you guys will object on what I did. =)
So out of curiosity, I tried to use reflection to change the mIsChild of type boolean private variable to false to behave as a root object and setting it to true to behave as a child object and it works. So I decided to combine the implementation of child and root object rather than implementing switchable object.
When calling Save() method in:
So instead of duplicating the code from Update() to DataPortal_Update(), I simply call Update() in DataPortal_Update().
Below are codes for switching editable object from root to child and vice versa:
Public Sub ToRoot(ByVal o As Object)
Dim t As System.Type
t = o.GetType().BaseType()
If String.Compare(t.Name, "BusinessBase", True) > 0 Then
Throw New Exception("Not a CSLA object")
End If
Dim fi As FieldInfo = t.GetField("mIsChild", _
Reflection.BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance _
Or BindingFlags.FlattenHierarchy Or BindingFlags.CreateInstance _
Or BindingFlags.SetField)
If fi Is Nothing Then
Throw New Exception("Unable to resolve CSLA object info")
End If
Dim IsChild As Boolean = fi.GetValue(o)
If Not IsChild Then
Throw New Exception("The object is already in Root")
End If
fi.SetValue(o, False)
End Sub
Public Sub ToChild(ByVal o As Object)
Dim t As System.Type
t = o.GetType().BaseType()
If String.Compare(t.Name, "BusinessBase", True) > 0 Then
Throw New Exception("Not a CSLA object")
End If
Dim fi As FieldInfo = t.GetField("mIsChild", _
Reflection.BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance _
Or BindingFlags.FlattenHierarchy Or BindingFlags.CreateInstance _
Or BindingFlags.SetField)
If fi Is Nothing Then
Throw New Exception("Unable to resolve CSLA object info")
End If
Dim IsChild As Boolean = fi.GetValue(o)
If IsChild Then
Throw New Exception("The object is already in Child")
End If
fi.SetValue(o, True)
End Sub
Scenario: Switchng editable object (from child to root) inside a root collection
eobjName = DirectCast(Me.BindingContext(mNameCol.Current, eobjName).Clone()
ToRoot(eobjName)
'Do something with root object and save changes
eobjName.Save()
ToChild(eobjName)
Friend Sub UpdateCollection(ByVal o as eobjNameType)
Dim i as Integer
For i = 0 To List.Count - 1
If DirectCast(List.Item(i), eobjNameType).Equals(o) Then
List.Item(i) = o
Exit For
End If
Next i
End Sub
Mediaspot's IMS Go Live
MediaSpot’s Inventory Management and Sales System go live. It did run with a couple of errors. LOL… It’s really different when you are the one who develop and at the same time doing the testing. I’m glad that it was my cousin’s store there was not too much pressure when the errors pop up. LOL.. Right now, I managed to finish all the bugs that were found.
There was a little delay on the time frame of the deployment of the project due to the fact that I was using CSLA .NET rather than doing in using datasets. I know that doing it datasets will give me the speed in development, but I wanted it to be in object oriented approach in dealing with the business process and another reason is that I’m a fan of Rockford Lhotka. LOL…
For the backend I was using MySQL latest version to take advantage with stored procedures, views and triggers. Why not MS-SQL? I guess the reason is clear MySQL is for free. =) But when I’ve found out the SQL 2005 Express is for free, it was too late because I was halfway doing it in MySQL. So my plan later is to create a database in SQL 2005 Express and modify the application to choose what type of backend database server to use.
I have no regrets using MySQL, at least I did have a chance to know SQL syntax in MySQL. The shift from MS-SQL to MySQL during developing the stored procedures wasn’t that hard. I’ve found some bugs in MySQL and I’ll be sending it MySQL to clarify if is really a bug or the feature is not supported.
MediaSpot is a store the sales a variety gadgets for cell phones, computers, digital cameras, DVD and MP3 players. It is located on JQS, which is beside UC Banilad Campus.