Salvo Davide Rapisarda

Italian developer thinks.

  • Linq2Sql: How to loop over context tables ?

    I use linq2sql in my project not using direct table mapping but using sql views as link. Sometimes (all depend by other collegues) there is a reason for modify/add a table field in db structure. Ok, I think it's normally but if the table is included in a view then my context goes "BOOM!!!".

    I have a context with 33 entities and every time i don't want to check one by one.

    Problem: How to check if every entity doesn't have mapping problem?

    Solution: Loop over every entities and call Any() method for each entity.

    Description: If, for each entity in context, call Any() method then I can found where there is a mapping problem,

    Code:

    Private Function DoTest()
            Dim properties() As PropertyInfo
            
            properties = GetType(MyDataClassesDataContext).GetProperties()
            For Each p In properties
                If p.PropertyType.IsGenericType Then
                    Dim attribs() As Object
                    attribs = p.PropertyType.GetGenericArguments()(0).GetCustomAttributes(GetType(TableAttribute), False)
                    If (attribs.Length > 0) Then
                        
                        If p.Name IsNot Nothing Then                                                                                         
                            resultList.Add(getItem( db.GetTable(DirectCast(db.[GetType]().GetProperty(p.Name).GetValue(db, Nothing), ITable).ElementType)))                       
                        End If
                        
                    End If
                End If
            Next

    End Function
    Private Function getItem(ByVal o As ObjectAs ResultItem
            Dim result As ResultItem
            Dim errmsg As String
            errmsg = ""
            If TryInvokeEntity(o, errmsg) Then
                result = New ResultItem With {.ViewName = o.ToString(), .Status = "OK", .ErrorMessage = ""}
            Else
                result = New ResultItem With {.ViewName = o.ToString(), .Status = "KO", .ErrorMessage = errmsg}
            End If
            
            Return result
    End Function
     
     
    Private Function TryInvokeEntity(ByVal source As IQueryableByRef errmsg As StringAs Boolean
            Dim first As MethodCallExpression
            
            first = Expression.Call(GetType(Enumerable), _
                                   "Any", _
                                   New Type() {source.ElementType}, _
                                   New Expression() {source.Expression})
            Try
                source.Provider.Execute(first)
                Return True
            Catch ex As Exception
                errmsg = ex.Message
                Return False
            End Try
            
            
    End Function
     
     

  • C# and VB.NET language specification

    Hi,

    If you search in your vs installation dir under subfolder Microsoft Visual Studio 10.0\VB\Specifications and Microsoft Visual Studio 10.0\VC#\Specifications you can found documents about language specificaiton.

    If you have 5 minutes to spend i think it's a good reading. ;-)

    Salvo.

  • SSMS Tools Pack

    SSMS Tools Pack is an add-in for Microsoft SQL Server Management Studio (SSMS) 2005, 2008, 2008 R2, Denali CTP3 and their respective Express versions.
    It contains a few upgrades to the SSMS IDE that I thought were missing.

  • Programming like making music

    This morning, while I was driving to work, a think was made in my mind. In this days I'm learning how to play classic guitar, and I was think: what kind of difference there are between make music and make software?. I think that there is no difference. If you compare music with programming, there are 7 notes (DO, RE, MI, FA, SOL, LA,SI) for making music while in computer programming we are a set of principal statements (IF,LOOPS,TYPES,ASSIGNMENTS)  for making software. If you make music you need a musical instrument (guitar, violin,ecc..) and a sequence of notes (for a predeterminated execution time). If you make software you need a good programming language as instrument (C#,VB.NET,JavaScript,ecc..) and make a sequence (in certain case a very long sequence :-)  ) of statements.