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.