Enable Code Analysis for all projects in the solution

Ralph Jansen BlogI needed a trick to enable code analysis for all my projects. I know you can set the rules in the “Analyze/Configure Code Analysis for Solution” window but didn’t find a way to enable or disable the CA for all the projects in one action. So I created a macro to do this. The macro could also change your target framework if you like. That is nice when you upgrading your project from framework 3.5 to 4.

 

 

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module ProjectUtilities

    Private Class ProjectGuids
        Public Const vsWindowsCSharp As String = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
        Public Const vsWindowsVBNET As String = "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}"
        Public Const vsWindowsVisualCPP As String = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"
        Public Const vsWebApplication As String = "{349C5851-65DF-11DA-9384-00065B846F21}"
        Public Const vsWebSite As String = "{E24C65DC-7377-472B-9ABA-BC803B73C61A}"
        Public Const vsDistributedSystem As String = "{F135691A-BF7E-435D-8960-F99683D2D49C}"
        Public Const vsWCF As String = "{3D9AD99F-2412-4246-B90B-4EAA41C64699}"
        Public Const vsWPF As String = "{60DC8134-EBA5-43B8-BCC9-BB4BC16C2548}"
        Public Const vsVisualDatabaseTools As String = "{C252FEB5-A946-4202-B1D4-9916A0590387}"
        Public Const vsDatabase As String = "{A9ACE9BB-CECE-4E62-9AA4-C7E7C5BD2124}"
        Public Const vsDatabaseOther As String = "{4F174C21-8C12-11D0-8340-0000F80270F8}"
        Public Const vsTest As String = "{3AC096D0-A1C2-E12C-1390-A8335801FDAB}"
        Public Const vsLegacy2003SmartDeviceCSharp As String = "{20D4826A-C6FA-45DB-90F4-C717570B9F32}"
        Public Const vsLegacy2003SmartDeviceVBNET As String = "{CB4CE8C6-1BDB-4DC7-A4D3-65A1999772F8}"
        Public Const vsSmartDeviceCSharp As String = "{4D628B5B-2FBC-4AA6-8C16-197242AEB884}"
        Public Const vsSmartDeviceVBNET As String = "{68B1623D-7FB9-47D8-8664-7ECEA3297D4F}"
        Public Const vsWorkflowCSharp As String = "{14822709-B5A1-4724-98CA-57A101D1B079}"
        Public Const vsWorkflowVBNET As String = "{D59BE175-2ED0-4C54-BE3D-CDAA9F3214C8}"
        Public Const vsDeploymentMergeModule As String = "{06A35CCD-C46D-44D5-987B-CF40FF872267}"
        Public Const vsDeploymentCab As String = "{3EA9E505-35AC-4774-B492-AD1749C4943A}"
        Public Const vsDeploymentSetup As String = "{978C614F-708E-4E1A-B201-565925725DBA}"
        Public Const vsDeploymentSmartDeviceCab As String = "{AB322303-2255-48EF-A496-5904EB18DA55}"
        Public Const vsVSTA As String = "{A860303F-1F3F-4691-B57E-529FC101A107}"
        Public Const vsVSTO As String = "{BAA0C2D2-18E2-41B9-852F-F413020CAA33}"
        Public Const vsSharePointWorkflow As String = "{F8810EC1-6754-47FC-A15F-DFABD2E3FA90}"
    End Class

    ” Defines the valid target framework values.
    Enum TargetFramework
        Fx40 = 262144
        Fx35 = 196613
        Fx30 = 196608
        Fx20 = 131072
    End Enum

    ” Change the target framework for all projects in the current solution.
    Sub ChangeTargetFrameworkForAllProjects()
        Dim project As EnvDTE.Project
        Dim clientProfile As Boolean = False

        Write("——— CHANGING TARGET .NET FRAMEWORK VERSION ————-")
        Try
            If Not DTE.Solution.IsOpen Then
                Write("There is no solution open.")
            Else
                Dim targetFrameworkInput As String = InputBox("Enter the target framework version (Fx40, Fx35, Fx30, Fx20):", "Target Framework", "Fx40")
                Dim targetFramework As TargetFramework = [Enum].Parse(GetType(TargetFramework), targetFrameworkInput)

                If targetFramework = ProjectUtilities.TargetFramework.Fx35 Or targetFramework = ProjectUtilities.TargetFramework.Fx40 Then
                    Dim result As MsgBoxResult = MsgBox("The .NET Framework version chosen supports a Client Profile. Would you like to use that profile?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, "Target Framework Profile")
                    If result = MsgBoxResult.Yes Then
                        clientProfile = True
                    End If
                End If

                For Each project In DTE.Solution.Projects
                    If project.Kind <> Constants.vsProjectKindSolutionItems And project.Kind <> Constants.vsProjectKindMisc Then
                        ChangeTargetFramework(project, targetFramework, clientProfile)
                    Else
                        For Each projectItem In project.ProjectItems
                            If Not (projectItem.SubProject Is Nothing) Then
                                ChangeTargetFramework(projectItem.SubProject, targetFramework, clientProfile)
                            End If
                        Next

                    End If
                Next
            End If
        Catch ex As System.Exception
            Write(ex.Message)
        End Try
    End Sub

    ” Change the target framework for a project.
    Function ChangeTargetFramework(ByVal project As EnvDTE.Project, ByVal targetFramework As TargetFramework, ByVal clientProfile As Boolean) As Boolean
        Dim changed As Boolean = True

        If project.Kind = Constants.vsProjectKindSolutionItems Or project.Kind = Constants.vsProjectKindMisc Then
            For Each projectItem In project.ProjectItems
                If Not (projectItem.SubProject Is Nothing) Then
                    ChangeTargetFramework(projectItem.SubProject, targetFramework, clientProfile)
                End If
            Next
        Else
            Try
                If IsLegalProjectType(project) Then
                    SetTargetFramework(project, targetFramework, clientProfile)
                Else
                    Write("Skipping project: " + project.Name + " (" + project.Kind + ")")
                End If
            Catch ex As Exception
                Write(ex.Message)
                changed = False
            End Try
        End If

        Return changed
    End Function

    ” Determines if the project is a project that actually supports changing the target framework.
    Function IsLegalProjectType(ByVal proejct As EnvDTE.Project) As Boolean
        Dim legalProjectType As Boolean = True

        Select Case proejct.Kind
            Case ProjectGuids.vsDatabase
                legalProjectType = False
            Case ProjectGuids.vsDatabaseOther
                legalProjectType = False
            Case ProjectGuids.vsDeploymentCab
                legalProjectType = False
            Case ProjectGuids.vsDeploymentMergeModule
                legalProjectType = False
            Case ProjectGuids.vsDeploymentSetup
                legalProjectType = False
            Case ProjectGuids.vsDeploymentSmartDeviceCab
                legalProjectType = False
            Case ProjectGuids.vsDistributedSystem
                legalProjectType = False
            Case ProjectGuids.vsLegacy2003SmartDeviceCSharp
                legalProjectType = False
            Case ProjectGuids.vsLegacy2003SmartDeviceVBNET
                legalProjectType = False
            Case ProjectGuids.vsSharePointWorkflow
                legalProjectType = False
            Case ProjectGuids.vsSmartDeviceCSharp
                legalProjectType = True
            Case ProjectGuids.vsSmartDeviceVBNET
                legalProjectType = True
            Case ProjectGuids.vsTest
                legalProjectType = False
            Case ProjectGuids.vsVisualDatabaseTools
                legalProjectType = False
            Case ProjectGuids.vsVSTA
                legalProjectType = True
            Case ProjectGuids.vsVSTO
                legalProjectType = True
            Case ProjectGuids.vsWCF
                legalProjectType = True
            Case ProjectGuids.vsWebApplication
                legalProjectType = True
            Case ProjectGuids.vsWebSite
                legalProjectType = True
            Case ProjectGuids.vsWindowsCSharp
                legalProjectType = True
            Case ProjectGuids.vsWindowsVBNET
                legalProjectType = True
            Case ProjectGuids.vsWindowsVisualCPP
                legalProjectType = True
            Case ProjectGuids.vsWorkflowCSharp
                legalProjectType = False
            Case ProjectGuids.vsWorkflowVBNET
                legalProjectType = False
            Case ProjectGuids.vsWPF
                legalProjectType = True
            Case Else
                legalProjectType = False
        End Select
        Return legalProjectType
    End Function

    ” Sets the target framework for the project to the specified framework.
    Sub SetTargetFramework(ByVal project As EnvDTE.Project, ByVal targetFramework As TargetFramework, ByVal clientProfile As Boolean)
        Dim currentTargetFramework As TargetFramework = CType(project.Properties.Item("TargetFramework").Value, TargetFramework)
        Dim targetMoniker As String = GetTargetFrameworkMoniker(targetFramework, clientProfile)
        Dim currentMoniker As String = project.Properties.Item("TargetFrameworkMoniker").Value

        If currentMoniker <> targetMoniker Then
            Write("Changing project: " + project.Name + " from " + currentMoniker + " to " + targetMoniker + ".")
            project.Properties.Item("TargetFrameworkMoniker").Value = targetMoniker
            project.Properties.Item("TargetFramework").Value = targetFramework
        Else
            Write("Skipping project: " + project.Name + ", already at the correct target framework.")
        End If
    End Sub

    Function GetTargetFrameworkMoniker(ByVal targetFramework As TargetFramework, ByVal clientProfile As Boolean) As String
        Dim moniker As String = ".NETFramework,Version=v"
        Select Case targetFramework
            Case ProjectUtilities.TargetFramework.Fx20
                moniker += "2.0"

            Case ProjectUtilities.TargetFramework.Fx30
                moniker += "3.0"

            Case ProjectUtilities.TargetFramework.Fx35
                moniker += "3.5"

            Case ProjectUtilities.TargetFramework.Fx40
                moniker += "4.0"

        End Select

        If clientProfile Then
            moniker += ",Profile=Client"
        End If

        Return moniker
    End Function

    ” Writes a message to the output window
    Sub Write(ByVal s As String)
        Dim out As OutputWindowPane = GetOutputWindowPane("Change Target Framework", True)
        out.OutputString(s)
        out.OutputString(vbCrLf)
    End Sub

    ” Gets an instance of the output window
    Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
        Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        If show Then win.Visible = True
        Dim ow As OutputWindow = win.Object
        Dim owpane As OutputWindowPane
        Try
            owpane = ow.OutputWindowPanes.Item(Name)
        Catch e As System.Exception
            owpane = ow.OutputWindowPanes.Add(Name)
        End Try
        owpane.Activate()
        Return owpane
    End Function

    ” Change the code analysis value for a project.
    Function ChangeCodeAnalysis(ByVal project As EnvDTE.Project, ByVal isCodeAnalysisEnabled As Boolean) As Boolean
        Dim changed As Boolean = True

        If project.Kind = Constants.vsProjectKindSolutionItems Or project.Kind = Constants.vsProjectKindMisc Then
            For Each projectItem In project.ProjectItems
                If Not (projectItem.SubProject Is Nothing) Then
                    ChangeCodeAnalysis(projectItem.SubProject, isCodeAnalysisEnabled)
                End If
            Next
        Else
            Try
                If IsLegalProjectType(project) Then
                    SetCodeAnalysis(project, isCodeAnalysisEnabled)
                Else
                    Write("Skipping project: " + project.Name + " (" + project.Kind + ")")
                End If
            Catch ex As Exception
                Write(ex.Message)
                changed = False
            End Try
        End If

        Return changed
    End Function

    ” Sets the code analysis for the project to the specified value.
    Sub SetCodeAnalysis(ByVal project As EnvDTE.Project, ByVal isCodeAnalysisEnabled As Boolean)
        Dim currentCAValue As String = project.ConfigurationManager.ActiveConfiguration.Properties.Item("RunCodeAnalysis").Value

        Dim targetCAValue As String
        If isCodeAnalysisEnabled Then
            targetCAValue = "True"
        Else
            targetCAValue = "False"
        End If

        If currentCAValue <> targetCAValue Then
            Write("Changing project: " + project.Name + " from " + currentCAValue + " to " + targetCAValue + ".")
            project.ConfigurationManager.ActiveConfiguration.Properties.Item("RunCodeAnalysis").Value = isCodeAnalysisEnabled
        Else
            Write("Skipping project: " + project.Name + ", already at the correct Code Analysis value.")
        End If
    End Sub

    Sub EnableOrDisableCodeAnalysisOnAllProjects()
        Dim project As EnvDTE.Project
        Dim clientProfile As Boolean = False

        Write("——— Enable or Disable Code Analysis on all projects ————-")
        Try
            If Not DTE.Solution.IsOpen Then
                Write("There is no solution open.")
            Else
                Dim codeAnalysisInput As String = InputBox("Enter 0 for disabling or 1 for enabling code analysis:", "Enabled", "0")

                Dim isCodeAnalysisEnabled As Boolean
                If codeAnalysisInput = 0 Then
                    isCodeAnalysisEnabled = False
                ElseIf codeAnalysisInput = 1 Then
                    isCodeAnalysisEnabled = True
                Else
                    Throw New InvalidOperationException("Wrong input. Only 0 or 1 is allowed")
                End If

                For Each project In DTE.Solution.Projects
                    If project.Kind <> Constants.vsProjectKindSolutionItems And project.Kind <> Constants.vsProjectKindMisc Then
                        ChangeCodeAnalysis(project, isCodeAnalysisEnabled)
                    Else
                        For Each projectItem In project.ProjectItems
                            If Not (projectItem.SubProject Is Nothing) Then
                                ChangeCodeAnalysis(projectItem.SubProject, isCodeAnalysisEnabled)
                            End If
                        Next

                    End If
                Next
            End If
        Catch ex As System.Exception
            Write(ex.Message)
        End Try
    End Sub
End Module

Default cascading delete in EF Code First

Ralph Jansen Blog

In the new Entity Framework 4.1 Code First you can easily create a database on your POCO objects. But the down side (sometimes) is that cascading delete is turned on by default.

Say you have two objects called Department and Manager.

image

image

A manager needs a department and a department can have multiple managers. This is done because we want to have a one-many relationship in this demo. To do this we can make navigation properties. A navigation property is created by the virtual keyword. If you want to create a many relationship, you should use the ICollection<> type.

image

image

If you generate the database now, the tables are generated and a foreign key relationship is made. To do this, EF has generated another column in your Manager table. The column name should be Department_Id. If you open the foreign key relationship you see that the cascading delete relationship is disabled.

But our POCO objects are not really complete. Because how would we know what the key of the Deparment is when I only want to query the Manager object? You can’t because you need the other object. To solve this, you can add a column that is similar as your generated Foreign key column. So, we change our object to this code:

image

If you generate your database again, you see that some things are changed. The automatically generated column Department_Id is no longer there and the DepartmentId column is now used for your foreign key relationship. Only, this is not the only difference in your project. Because if you look at your foreign key relationship, you see that the cascading delete is now enabled.

But why do we want a cascading delete in this example? Because say in example, that if a department is merged with another department and the department record would be deleted from the database, your managers would be deleted as well. Why will they be deleted? Are the fired?!?!?! Can’t they go to another department?

Luckily you can change this default behavior. You can do this with the next line of code in your OnModelCreating override:

image

If you generate your database again with the latest classes (class with DepartmentId) your Foreign Key relationship would not have a cascading delete enabled.

Entity Framework: Different loading capabilities

Ralph Jansen BlogThe Entity Framework is in the new stage of evolving. We are now in stage 4.1 where the Code First functionality is released. Entity Framework enables just like LINQ to SQL different kind of loading types. But what are the loading types and what are the different capabilities? In this blog I will explain in a simple way what they are and what they can do.

There are 3 different kind of loading types for related data called lazy, Eager and explicit loading. In the next example you see two entities which are related.

image

Department can have many Courses and a Course can only have one Department.

Lazy Loading:
image 

 

Eager Loading:
image

 

Explicit Loading:
image

Because they don’t immediately retrieve the property values, lazy loading and explicit loading are also both known as deferred loading.

In general, if you know you need related data for every entity retrieved, eager loading offers the best performance, because a single query sent to the database is typically more efficient than separate queries for each entity retrieved. For example, in the above examples, suppose that each department has ten related courses. The eager loading example would result in just a single (join) query. The lazy loading and explicit loading examples would both result in eleven queries.

On the other hand, if you need to access an entity’s navigation properties only infrequently or only for a small portion of a set of entities you’re processing, lazy loading may be more efficient, because eager loading would retrieve more data than you need. Typically you’d use explicit loading only when you’ve turned lazy loading off. One scenario when you might turn lazy loading off is during serialization, when you know you don’t need all navigation properties loaded. If lazy loading were on, all navigation properties would all be loaded automatically, because serialization accesses all properties.

HTML klaar voor het grote werk?

Als ik kijk naar de volwassenheid van het huidige web is HTML zelf nog niet helemaal klaar voor het grote werk. Als ik kijk naar HTML 5 in combinatie met frameworks als JQuery, Modernizr etc. zeg ik dat het de goede kant op gaat. De laatste versies van alle browsers werken goed maar er zijn nog steeds een hoop verschillen in functionaliteit die worden ondersteund. Microsoft zegt dat IE9 helemaal het einde is maar ze weten dat ze snel achter zullen lopen. Microsoft zegt zelf dat er geen release cyclus van +/- 6 weken komt voor IE maar ondertussen is de eerste versie van IE10 al in zicht. Ze zullen wel moeten want de concurrenten gaan veel sneller. Ik denk dat we op een gegeven moment ook alleen maar horen over IE en niet meer IE x. Weet iemand uit zijn hoofd wat de laatste versie van Chrome ondertussen is? Pssst antwoord is 11 sinds deze week.

Alle features van HTML 5 zijn nog lang niet bekend. Dit kan nog wel een tijd gaan duren. Tot die tijd zullen we moeten checken op features door middel van frameworks. Als ik verder kijk dan alleen de computer zal HTML bij mij toch echt de voorkeur krijgen. De tablet oorlog is volgens de analisten pas dit jaar van start gegaan. Het aantal tablets bij de consumenten en bedrijven (ik hoor het steeds meer om me heen bij bedrijven) groeit met een rap tempo. Het aanhouden van een standaard is dan wel zo makkelijk.

Ontwikkelmethodieken als MVC stellen je in staat logica en presentatie makkelijker te scheiden. Het compatible maken/ondersteunen van je applicatie voor verschillende platformen is dus ook een stuk eenvoudiger. Je bereikt hierdoor een groter publiek en zal dus meer omzet genereren. De techniek staat niet stil waardoor we onze strategieën zelf moeten beslissen maar wat je wel ziet is dat de doelgroep of manier van werken langzaam wijzigt. De IPad 2 is uit en meteen heel het internet staat vol met tweede hands IPad 1 omdat die oud is. Er is veel meer vraag naar nieuwe techniek en alle informatie dient voor alle gadgets en platformen aanwezig te zijn. Mensen verwachten tegenwoordig dat dit ook zo wordt gemaakt.

De smartphoneverkoop neemt in een jaar tijd met 83% toe. Het aantal verschillende telefoons op de markt met verschillende specificaties neemt ook rap toe. Het gebruiken van standaarden begint nu dus echt belangrijk te worden. Grote organisaties als Microsoft, Google en Apple omarmen dit ook. Het is niet voor niets dat grote organisaties steeds meer open source projecten financieel ondersteunt zodat hier de community weer gebruik van kan maken. Soms worden projecten door dit wel is stop gezet. Denk maar aan de AJAX techniek van Microsoft. Microsoft ontwikkelt zelf nu voor JQeury. Heel het templating in JQuery komt namelijk van Microsoft.