May 2011 - Posts

Never SELECT * from Entity Framework called Stored Procedures

The Entity Framework can help us create an efficient Data Access Layer or a horribly inefficient one. 

It's worth mentioning that if we use Entity Framework to call a stored procedure that returns a database derived Entity using a SELECT * (star), we will have that horribly inefficient DAL.  Of course, as we've been told for years, SELECT * is 99.0% wrong with 1% for those dynamic field cases.  With EF calling a stored procedure, it's really wrong.

SQL Profiler will show us that the store procedure does return all records as fast as it can, barring a badly written stored procedure or low SQL server memory.  Entity Framework unfortunately does not assume all fields have returned.  EF then calls back with the defined primary key for each record.  This means that for a stored procedure that returns 1 million rows in one connection, actually returns 2 million rows with 2 million and one database server calls / connections from the application layer.  Try it on a 3 record table sometime to see that it calls SQL Server 4 times for three rows even when we use a stored procedure.  Enterprise DBA's will complain as they should.  If there is an obscure switch to avoid this, I couldn't find it.

The Moral of the Story:

  • Always define fields from the SELECT statement even if SQL 2005 and greater does optimize it for us.
  • Use SQL Profiler to look at the number of calls and the I/O count of each call.

-Vince

Posted by vblasberg with no comments

Containing Silverlight Lists and DataGrids in the Browser Window

In a typical Silverlight line-of-business application we have Lists, Grids, DataGrids, and StackPanels.  We populate a list and it flows down and off the browser page.  When we have a ScrollViewer, it will scroll the whole page including edit controls and graphics and not just the list that's tall and flowing off the page.  The good news is that we can easily contain the list in the viewable area with the few simple steps listed below.

  1. Remove the ScrollViewer from the page if it exists.
  2. Contain all upper area graphics and edit controls in a RowDefinitions of explicit size or Auto.
  3. Contain the lower List(s) in a final Grid RowDefinition with the Star notation to fill the remainder of the window using the outer-most Grid as a parent container.
  4. Optionally specify for the List properties,  VerticalAlignment="Top" and VerticalContentAlignment="Stretch".  Otherwise the List and its borders will stretch to the bottom of the screen even if it has no items.  Either way may be preferred. 

The following screen shots demonstrate this with a simple GridSplitter to size either side.  Notice the List on the left is using two rows compared to the list on the right.  The list on the right isn’t flowing to the bottom and uses only one row.  Both lists flow to the bottom of the browser window and no further, regardless of the number of items.

XAML rocks
.
-Vince


Runtime:

  

Designer:

  

 

XAML:

    <Grid x:Name="LayoutRoot"
          Background="#FFCEA1A1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <sdk:Label Content="List 1"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   FontSize="16"
                   FontWeight="Bold" />
        <sdk:Label Grid.Column="2"
                   Content="List 2"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   FontSize="16"
                   FontWeight="Bold" />
        <ListBox Grid.Row="1"
                 Grid.RowSpan="2"
                 Name="listBox1"
                 Margin="10" />
        <sdk:GridSplitter Grid.Row="1"
                          Grid.Column="1"
                          Width="10"
                          HorizontalAlignment="Stretch"
                          Grid.RowSpan="2" />
        <ListBox Grid.Row="2"
                 Grid.Column="2"
                 Name="listBox2"
                 Margin="10"
                 VerticalAlignment="Top"
                 VerticalContentAlignment="Stretch" />
        <sdk:Label Grid.Row="1"
                   Grid.Column="2"
                   Content="(Edit Controls)"
                   FontSize="16"
                   FontWeight="Bold"
                   HorizontalAlignment="Center" />
    </Grid>
</UserControl> 


Code-Behind:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    listBox1.Items.Clear();
    listBox2.Items.Clear();

    for (int i = 1; i <= 100; i++)
    {
        string listItem = "List Item " + i.ToString();

        listBox1.Items.Add(listItem);
        listBox2.Items.Add(listItem);
    }
}

 

 

Posted by vblasberg with no comments
Filed under: ,
More Posts