Linq to SQL

Requirment:

Nothwind Sql Database

Advantages of Linq to SQL

While everything with Linq to Sql ca be done with Ado.Net, there are a few advantages

  • Less code: you don't need to write ADO.NET code for querying the database. You can use a tool to generate the data classes you need.
  • Flexible querying capabilities: Rather than struggle with SQL, you can use the LINQ querying model. Ultimately, you'llbe able to use one consistent model (LINQ expressions) to access many different types of data, from databases to XML.
  • Change tracking and batch updates: This is the most important one for me, (because I already have ado.net tools and i don't mindwriting sql) You can change multiple details about the data yo've queried and commit a batch update. 

Add a new linq to sql item to your project 

 

 

 Sample usage

 

[code:c#]

class Program
{
    static void Main(string[] args)
    {
        Program prog = new Program();
        prog.ListAllOrderProducts();
        prog.ListAllOrderProductsForCustomer();
        Console.ReadLine();
    }


    private void ListAllOrderProducts()
    {
        CustomerOrdersDataContext dc = new CustomerOrdersDataContext();
        Table<Order> table = dc.GetTable<Order>();
        foreach (var order in table)
        {
            Console.WriteLine(order.Order_Details[0].Product.ProductName);
        }           
    }

    private void ListAllOrderProductsForCustomer(string custId = "ALFKI")
    {
        CustomerOrdersDataContext dc = new CustomerOrdersDataContext();
        var result =
            from order in dc.GetTable<Order>()
            where order.CustomerID == custId
            select order;
                   
        foreach (var x in result)
        {
            Console.WriteLine(x.Order_Details[0].Product.ProductName);
        }
    }
}

[/code]

Anonymous delegates and event execution

A nice little trick to save you always checking for null when firing custom events
Initialize to anonymous delegate.
Ok.. we've an extra call in the invocation list so use judiciously

[code:c#]

public event CompleteTaskExecutionHandler CompleteExecution = delegate { };

//or a sample using Lambdas

public event PropertyChangedEventHandler PropertyChanged = (s,p) => { };

 

 

private void Fire()
{
   this.CompleteExecution(...);
}

 

[/code]

Extension Method for Wpf Window with Froms Owner

 

Extension Method

[code:c#]

internal static class InteropExtensions
{
    public static bool? ShowDialog(this System.Windows.Window win, IntPtr handle)
    {
        WindowInteropHelper helper = new WindowInteropHelper(win);
        helper.Owner = handle;
        return win.ShowDialog();
    }
}

[/code]

 

Usage

[code:c#]

var win = new WpfWindow();
win.ShowDalog(windowsFormOwnerHandle);

[/code]

Sql Server Compact Edition

I've been playing with a workflow service hosted here /PSService/PSService.svc
Feel free to give it a bash [August 2012, removed]) Endpoint is using basic http binding.

Tonight I whipped a Sql Server Compact Database out of another little app I have lying around so I could sit it behind the webservice persist data.

But

To my horror it doesn't work..

I get the following exception " sql compact is not intended for asp.net development"

I can imagine why I guess.. bit what a pity.. it's not allowed..

[August 2012: It is allowed, just needs a setting]

WPF Grid splitters

Addin splitters to the WPF grid couldn't be easier.

Veritcal

<GridSplitter />

Horizontal

<GridSplitter Height="3"
   ResizeDirection="Rows" Background="Transparent"
   HorizontalAlignment="Stretch"
   VerticalAlignment="Stretch" Grid.Row="1" />

WPF Routed events

Ever have a control on a window and want to be able to receive button clicks from this child control?
More than one I've done this the hard way, but now.....

 

[code:c#]

public VariablesWindow()
  {
   this.InitializeComponent();
   variables.AddHandler(Button.ClickEvent, new RoutedEventHandler(HandleChildClick));
  }
  
  private void HandleChildClick(object sender, RoutedEventArgs args)
        {
            Button senderButton = args.OriginalSource as Button;
            if (senderButton != null && senderButton.Content != null)
            {
                string buttonText = senderButton.Content as string;
                if (buttonText == "_Cancel")
                {
                    this.DialogResult = false;
                    Close();
                }
                else if (buttonText == "_OK")
                {
                    this.DialogResult = true;
                    Close();
                }
            }
        }

[/code]

Key modifiers

Recently I used PInvoke to check if the SHIFT key was pressed while i was doing a drag operation......

what I should have done then and have done now is

 

[code:c#]

if((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.None)
  Trace.WriteLine("Shift is pressed");

[/code]

A Custom Diagramming Library

Recently I've been working with a 3rd party diagramming library written in windows forms. It's been great and it's pretty fast in building 80% of the application I'm working on.

[Update August 2012: I never got around to doing much with the diagramming library. I opted instead to re-host the visual studio workflow designer for my needs, why I actually considered writing it is beyond me, I’m a campaigner for bolting existing solutions together rather than re-inventing the wheel ]