Since most of the applications that I create are distributed accross a WAN that spans the country I am really big about minimizing the bandwidth usage of my applications. That typically means serializing and compressing the large chunks of data that go across the wire.
My most recent application is using LINQ to SQL. However, its only using Stored Procedures since that was a requirement passed down "from above". Today I wanted to start minimizing that bandwidth footprint of my application and serialize then compress the data into a byte array. When I tried to do this I was shocked to notice that the classes that are generated when adding a Stored Procedure to the Object Relational Designer are not marked with any kind of serializable attribute.
After digging for a bit I found out that LINQ to SQL doesn't support binary serialization, which is what I wanted. I came across this nice extension by WinSpace that may have worked for me: http://www.codeproject.com/KB/linq/linqsqlserialization.aspx
Instead of implementing that and having to change code I remembered that I could just add the attribute manually to the DBML designer class. Modifying those auto generated files isn't the best idea so instead I created a separate file that housed partial classes for each of the auto generated classes. I added the serializable attribute to those and everything worked.
For example in the auto generated "DataClasses1.designer.vb" after dragging a sproc into the designer you get a partial class something like the following:
Code Snippet
Partial Public Class MyDataRetrieveResult
Private _Name As String
Public Sub New()
MyBase.New()
End Sub
<Column(Storage:="_Name", DbType:="VarChar(100) NOT NULL", CanBeNull:=False)> _
Public Property Name() As String
Get
Return Me._Name
End Get
Set(ByVal value As String)
If (String.Equals(Me._Name, Value) = False) Then
Me._Name = Value
End If
End Set
End Property
End Class
In a separate class file all you need to do is create another partial class stub and add the serializable attribute:
Code Snippet
<Serializable()> _
Partial Public Class MyDataRetrieveResult
End Class
Then you can do custom binary serialization without it blowing up.
I am currently working on a .Net Windows Forms MDI application. On the MDI container I added a panel docked to the left side of the form and added some code that allows the user to click its border and re size it. However, after rearranging some controls and adding a couple of new controls the layout stopped working correctly. Every time I opened a child form it was displayed behind the panel. It wasn't doing that previously and its been a couple of weeks since I got everything working the way I wanted it and I couldn't remember exactly what I had done.
After
googling "MDIchild form goes behind the panel" I knew the panel needed to be docked. For some reason it wasn't anymore so I re docked it to the left side of the form only to have all my menus pushed over to the right of the control as seen in the example below. Previously when I docked it to the left of the form it only went up to the menu containers which looks normal.
After spending way more time on this than I should have, I stopped googling since I wasn't finding anything useful and instead googled my brain. Because a good majority of my time for the last few years has been spent doing ASP.NET applications I thought about how a web page is laid out and how the order of the controls can affect the styling.
Finally, on a whim, I found and opened the Document Outline Window (example below) from the View Menu in Visual Studio, and pushed the panel up in the order. That did the trick. Somehow the order of the controls got all out of whack.
The lesson I learned was to always have the document outline window open when working in the form designer. If it was open by default I could have spent those 2 hours of my life more productively.
It is my theory that good developers are lazy at heart. You would think that is a bad thing most of the time but in reality it can really come in handy.
Here is an example.
Lets say you have a object set up that holds about 15 different parameters for a report query. Now lets say some exception is thrown while its quering the data and you need to email IT Operations to notify them that something broke. You know it might be helpful to track the problem if you could see what data was passed in to the query, but there are just too many parameters to manualy add to that email.
Here comes reflection to rescue. Now you have a generic method, in this case an overloaded ToString method, that loops through all of the properties in the object and returns a formated string that can easily be added to that exception email.
Public Overrides Function ToString() As String
Dim ret As String = String.Empty
Dim p As System.Reflection.PropertyInfo() = Me.GetType.GetProperties()
For Each prop As System.Reflection.PropertyInfo In p
Dim strMyPropertyValue = prop.Name & ": " & prop.GetValue(Me, Nothing).ToString
ret &= strMyPropertyValue & vbCrLf
Next prop
Return ret
End Function
Of course this could be a bit more robust and do some type checking to see if the properties had properties of thier own, but in this instance all of the properties were simple types.