.NET Concurrency

Optimistic Concurrency

In a multiuser environment, there are two models for updating data in a database: optimistic concurrency, and pessimistic concurrency. The DataSet object is designed to encourage the use of optimistic concurrency for long-running activities such as when you are remoting data and when users are interacting with data.

Pessimistic concurrency involves locking rows at the data source to prevent users from modifying data in a way that affects other users. In a pessimistic model, when a user performs an action that causes a lock to be applied, other users cannot perform actions that would conflict with the lock until the lock owner releases it. This model is primarily used in environments where there is heavy contention for data, where the cost of protecting data with locks is less than the cost of rolling back transactions if concurrency conflicts occur.

Therefore, in a pessimistic currency model, a user who reads a row with the intention of changing it establishes a lock. Until the user has finished the update and released the lock, no one else can change that row. For this reason, pessimistic concurrency is best implemented when lock times will be short, as in programmatic processing of records. Pessimistic concurrency is not a scalable option when users are interacting with data, causing records to be locked for relatively large periods of time.

By contrast, users who use optimistic concurrency do not lock a row when reading it. When a user wants to update a row, the application must determine whether another user has changed the row since it was read. Optimistic concurrency is generally used in environments with a low contention for data. This improves performance as no locking of records is required, and locking of records requires additional server resources. Also, in order to maintain record locks, a persistent connection to the database server is required. Because this is not the case in an optimistic concurrency model, connections to the server are free to serve a larger number of clients in less time.

In an optimistic concurrency model, a violation is considered to have occurred if, after a user receives a value from the database, another user modifies the value before the first user has attempted to modify it.

 

LINQPad

If you've not used nor know what LINQPad is...... tut tut tut

Download it form here

http://www.linqpad.net/  it will make your life easier, infact you'll wonder whatever you did without it...

So how does it help...? try it and see :-)

Declarative Ria Data and Controls

I'm really loving this declarative approach with silverlight and wpf... (ask me why and I can't tell you ! :-)

Anyway I've just stumbled across a way of managing RiaDataContexts Declaratively
I found it on the Telerik samples.... If you've not looked at these guys controls then check them out!!

 

[code:c#]

<navigation:Page xmlns:dataFormToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"  xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
  x:Class="SiteDocs.Loler"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
  xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
  xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Ria"
  xmlns:e="clr-namespace:SiteDocs.Web.Services"
  xmlns:riaData="clr-namespace:System.Windows.Data;assembly=System.Windows.Controls.Ria"
  mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" 
  Style="{StaticResource PageStyle}"
>

  <Grid x:Name="LayoutRoot" >
    <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}" >

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
    
    <Grid x:Name="gridLolerLeft" >
     <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition Height="Auto"/>
     </Grid.RowDefinitions>

                    <riaControls:DomainDataSource x:Name="DomainDataSource1" AutoLoad="True" QueryName="GetLolers" PageSize="10">
                        <riaControls:DomainDataSource.DomainContext>
                            <e:LolerContext />
                        </riaControls:DomainDataSource.DomainContext>
                        <riaControls:DomainDataSource.FilterDescriptors>
                            <riaData:FilterDescriptorCollection LogicalOperator="Or" />
                        </riaControls:DomainDataSource.FilterDescriptors>
                    </riaControls:DomainDataSource>

                    <telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding Data, ElementName=DomainDataSource1}"
                             Filtering="RadGridView1_Filtering" IsBusy="{Binding IsBusy, ElementName=DomainDataSource1}" />
           <telerik:RadDataPager x:Name="RadDataPager1" Grid.Row="1" Source="{Binding Data, ElementName=DomainDataSource1}" DisplayMode="FirstLastPreviousNextNumeric, Text" IsTotalItemCountFixed="True"/>

    
    </Grid>

                </Grid>
    </ScrollViewer>
  </Grid>

</navigation:Page>

[/code]

Expression blend visual states

A few people have asked me what's the easiest way of doing transitions on Silverlight.

One of the easiest ways has to be to use the VisualStateManager with Expression Blend, see screen show for sample logged in state.
If you don't know how to use this tool then start watching a few vids!

 

To change between states you can use this code..

 

 

[code:c#]

if (WebContext.Current.User.IsAuthenticated)
{
    VisualStateManager.GoToState(this, (WebContext.Current.Authentication is WindowsAuthentication) ? "windowsAuth" : "LoggedIn", true);
}
else
{
    VisualStateManager.GoToState(this, "LoggedOut", true);
}


[/code]

Providing Security in RIA services

If you wish to prevent clients accessing your data

[code:c#]

[RequiresAuthentication]
[EnableClientAccess()]
public class LolerService : LinqToSqlDomainService<LolerModelDataContext>

[/code]

Popups in wpf

Here is come code to show a popup in wpf, i've nothing in the popup at the moment, just a gradient background and border.

[code:c#]

<Popup Name="popup1" 
   Width="{Binding ElementName=bdrCalendar, Path=ActualWidth, Converter={StaticResource MarginValueConverter}}"
   Height="150"
   Placement="Center"
   PopupAnimation="Scroll"
   AllowsTransparency="True"
   PlacementTarget="{Binding ElementName=bdrCalendar}"
   MouseDown="popup1_MouseDown" >
 <ctrls:PopupContent />
</Popup>

 

[/code]

 

 

 

RIA Services

Over the w/end I was helping a mate with his Data-driven Silverlight app.

I was gobsmacked that his developer spent days and weeks writing plumbing code, hand coding infrastructure code and even worse my friend had to pay for it.
Being able to focus on the business needs is important so having a development platform that better enables that focus and increased productivity is an absolute must.

The solution to this problem is codenamed Alexandria i.e. .NET RIA Services, this technology provides a set of server components and ASP.NET extensions that ease the n-tier development process, making your applications almost as easy to develop as if they were running on a single tier, In addition, services such as authentication, roles and profile management are provided. The combination of client and server enhancements to Silverlight 3 and ASP.NET along with the addition of .NET RIA Services, streamline the end-to-end experience of developing data-driven Web applications also known as Rich Internet Applications, or RIAs.

I’m not going to show you how to do it as there are many-many training resources available but a good place to start is here: http://silverlight.net/getstarted/riaservices/

So if you find yourself writing needless plumbing code for a DAL in Silverlight, then spend some time figuring RIA services out!
(Spend some get some diesel for that digger and opposed to grabbing a shovel and diving in!!)

 

 

Filtering data in Silverlight

Ever want to filter data in Silverlight? here's a simple example that uses a lambda expression to search on name (case sensitive)

 

[code:c#]

ComboBox cbx = ((ComboBox)sender);

ICollectionView dataView =
            CollectionViewSource.GetDefaultView(this.DataContext);
if (!string.IsNullOrEmpty(cbx.Text))
    dataView.Filter = f => ((Job)f).Name.Contains(cbx.Text);
else
    dataView.Filter = null;

[/code]

Cocoa Is My Girlfriend

Tonight I've stumbled across an interesting Blog on cimgf.

http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/#more-909

It's been many years since I wrote PHP, so I'd naturally use asp.net serverside (or if i had a pc powerfull enough I'd use the new REST capabilities of Sharepoint 2010 server).

I've been dying to have a play with Azures for sometime now but I'm always either snowed under with work or playing with some other technology, I remember reading that Azures (storage at least) has a REST API, and with the launch of the iPhone on the Vodafone network at the end of this month I've been looking into what's involved in writing objective C (and some mono stuff) so it seems I may finally get to have a play :-)

 

Silverlight 4 Databinding and VS2010

If you've got a data driven application, databinding is the infrastructure of choice that makes the link between data and your UX

Silverlight 4 has a few improvments that brings it closer to WPF

  • TargetNullValue  - lets you specify the what to display in the target when the source value is null
    e.g.  {Binding Path=Name, TargetNullValue=NoName}
  • StringFormat - specify how a strnig should be formatted
    e.g.  {Binding Path=Salary, StringFormat=C}
  • FallbackValue - I love this one, comes in pretty handy when dealing with polymorphic classes where a you know in advance what properties specialized classes have and want to display them... or display a fallback value if they don't exist.
    e.g.  {Binding Path=LastLoggedOn, FallbackValue=Never}