Azure: SQL connection between servers

I’m running the D2 VSU service, and right now I’ve added SqlServer 2014 to this box. The localdb dialog in Management studio – note the server name of [(localdb)\projects]. And also keep

Connect01

Back in VSU 2013, the server explorer knows about Azure, and prompted me for logon  to my VMs:

Connect02

Now turning back to the LocalDB:

Connect03  sadf

And back in SSMS, SQLServer now knows about the localdb (and I got into this via Windows Auth):

Connect04

Now I go for the EF example on LocalDb:

Connect05

 

Connect string is as simple as this:

Data Source=(localdb)\projects;Initial Catalog=NorthWind;Integrated Security=True

Connect06

 

Choice of EFs:

Connect07

Pick a nice simple single table which has no dependencies:

Connect08

 

Doh – picked EF 6 when I meant 5. Never mind, won’t go back: this is interesting:

Connect09Now Now all of this is allegedly running on an SDD, so interesting the queue length is so high:

Connect10

 

Going back to get-help, this isn’t promising, especially as running on PS 4.0:

Connect11

never mind, not needed right now. We haveConnect12 our simplest possible model, but we can build the Web Application, although right now, there is no Silverlight or other client to generate for. The CSDL in the Solution Explorer. Note that the Code Gen Strategy has a different default from earlier versions (see my other posts on EF code gen):

Connect13

Now we add a client project – I’m lazily using default names, I just want to prove it finally flows end to end:

Connect14

 

Use the existing web app, and enable WCF RIA services. Note that I didn’t have to load any extra libraries for the RIA services, or toolkit. I’m doing so many screenshots because this is VSU 2013, and I want to see the differences with 2012, which is my usual VS.

Connect15

 

Hm, no client code generated, apart from a WebContext. Hang on, I wasn’t prompted for which tables to code gen for. Let’s revisit that, then.

OK, still haven’t got to the bottom of this, other than EF6.0 on VS2013 does not want to seem to play ball. I could use the Open RIA library, but I don’t want to, as this shold be doable within VS.

 

 

 

 

 

 

 

 

PowerShell – file handling examples

So that I have some consistent data for playing with, I’ve created a functions script file, imaginatively called “FileFunctions.ps1”.
The data examples in it are very loosely driven from the NorthWind structure, again so that we have a stab at a common model and data (others have noted how much they might hate NorthWind, but there is nothing more useful as a common currency).
This then is the first go, with a, very loosely termed, suppliers file. Yes, it’s tiny right now, let’s see how fit for purpose it is as I try out examples on it, now and in the future.
The input:
SupplierFile01
, and the output:

SupplierFile02

The code:


<#
.Synopsis
   Write an example fixed-width file based on Northwind Suppliers data.
   I've added a date column to add 1 extra datatype to test against.
   The columns are smaller than those on the database to allow you to see it all in one-ish go.
.Description
   Format: name length start-position targetdatatype
           SupplierID 5 1 int
           CompanyName 10 6 string
           ContactName 10 16 string
           FirstContractDate 10 26 date format dd/mm/yyyy

.Example
    Create-SuppliersFile -FileName .\suppliers.txt
#>
function Create-SuppliersFile
{
    Param (
        [Parameter(Mandatory=$true)]
        [string]
        $FileName
    )
    Begin {
        Set-StrictMode -Version latest
        $WriteFile = '$lines[$i] | out-file -Append -FilePath $($FileName) -Encoding ascii'

        [string[]] $lines = $null
        $lines += "         1         2         3         4"
        $lines += "1234567890123456789012345678901234567890"
        $lines +=  "00001" + "Smiths    " + "Joe Hat   " + "22/12/2013"
        $lines +=  "00002" + "Deb Cakes " + "Seb Soames" + "02/03/1987"
        $lines +=  "00003" + "Jim Bikes " + "Tom Jones " + "01/06/2001"
    }
    Process {
        Remove-Item $FileName
        for ($i = 0; $i -lt $lines.length ; $i++) {  invoke-expression $WriteFile }
    }
    End {
    }
}

Now let’s say we want to get the min, max and average lengths of the records in this file. Predictably, there is a PowerShell for that (did you see what I did there?):

(Get-Content .\suppliers.txt) | measure-object -property length -minimum -maximum -average

SupplierFile03

Now let’s say that we want to define an expected length for each of these records, and report when any records are not that value. I can’t remember or find if the current pipeline object variable ($_) has some current count value. So in its place let’s just initialize $i at the start of each run (its life continues after you execute the command, unless you explicitly tidy up). That all gives you this or somesuch:

SupplierFile04

$i = 0; (gc .\suppliers.txt) | % {$i++; if ($_.length -ne $expectedLength) {"[$($_.length)] at line [$i] "}}

Of course, what I’m doing here is jolly poor practice, namely abusing the shortcuts to fit stuff on one line. Decode:

gc => get-content
% => for-each
$_ => current pipeline object

Actually I think the other stuff is OK. And finally for this session, let’s say you recognise/know that the first 2 lines are header records to be ignored for your immediate purposes, and you want just the ContactName column, which you know is at (zero-based) offset 15, for 10 characters:

SupplierFile05


$i=0; (gc .\suppliers.txt) | % {if ($i++ -gt 1) { $_.substring(15,10) }}

Dear me, I forgot for a moment, you don’t need to go to all that pain, you can just skip over the records to ignore (and there is also -Take, but that’s for another day):

SupplierFile07


gc .\suppliers.txt | Select-Object -Skip 2 | % { $_.substring(15,10) }

As you might anyway want this tiny data file to play with, this is the content, as you know. Having tiny data like this is great for getting focus on the syntax and semantics of any new language, ain’t it though:

SupplierFile06


1 2 3 4
1234567890123456789012345678901234567890
00001Smiths Joe Hat 22/12/2013
00002Deb Cakes Seb Soames02/03/1987
00003Jim Bikes Tom Jones 01/06/2001

(Sorry, the WordPress formatter doesn’t seem to play ball with the spaces.)

Commanding in Prism

commanding01

In the screenshot below, the button is enabled because the text box does not contain ‘NO’:

commanding02

In the screenshot below, the button is disabled because the text box does contain ‘NO’:

commanding03

The comments in the code screenshots probably cover everything:

commanding04

commanding05

commanding06


using System.Windows;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System.Windows.Input;

namespace CommandImpl.ViewModel {

///
/// A basic test of the Prism implementations of Commanding, using DelegateCommand and NotificationObject.
/// http://msdn.microsoft.com/en-us/library/gg431410(v=pandp.50).aspx
// http://msdn.microsoft.com/en-us/library/system.windows.input.icommand(v=vs.110).aspx
// http://jeremybytes.blogspot.co.uk/2012/12/five-months-in-working-with-prism-in-wpf.html
// http://msdn.microsoft.com/en-au/library/ff921102.aspx
// http://msdn.microsoft.com/en-us/library/gg419054(v=pandp.50).aspx
/// Next steps are how to test this.
///

public class CommandViewModel : NotificationObject {

private ICommand _submitCommand;
public ICommand SubmitCommand {
get { return _submitCommand; }
set { if (_submitCommand == value) return;
_submitCommand = value;
RaisePropertyChanged(() => SubmitCommand);
}
}

public CommandViewModel() {
SubmitCommand = new DelegateCommand(Submit, CanSubmit);
}

public bool CanSubmit(string cmdParameter) {
if (cmdParameter == null) return true;
return !(cmdParameter.Contains(“NO”));
}

private static void Submit(string cmdParameter) {
// Although I only want the command parameter for the CanSubmit, it appears there is no ctor
// that takes no parameter for the Execute, but a parameter for the CanExecute
// The idea is possibly I guess that you pass-through the value to the CanExecute method.
// And the plus anyway is that if CanSubmit returns false, you never reach here anyway.
// But given that, it does mean that cmdParameter is always redundant, as you clearly do not invoke
// CanSubmit from here.
var msg = string.Format(“Value in textbox: [{0}]”, cmdParameter);
MessageBox.Show(msg);
//msg = CanSubmit(cmdParameter) ? “Can execute” : “Cannot execute”;
//MessageBox.Show(msg);
}
}
}

Resharper Live Templates

(My YouTube video on ReSharper templates is here.)

I sometimes use expandable snippets in Visual Studio, eg prop, cw, but as I have the luxury of ReSharper, I really ought to look at what that can do for me. Here seems a reasonable starting point:

Resharper01

So it’s not perfect, why would it be, and I was possibly a little ambitious in that I tried to template a whole file first. This is my scenario: In EF / RIA, I want to template the viewmodel class(es) that use the generated proxy classes, so that any additional work really is specific to the individual need at the time. That is of course just a definition of what a template is all about anyway.

Putting aside the whole file thing, and cutting back to something more modest, my first pass is this:

template01

Significant things (this works for me):

  • Its shortcut name is myla
  • Uncheck the default format box – I want to keep things how I’ve formatted them
  • The word ENTITY gets replaced at paste-time with your chosen parameter
  • The word LC_ENTITY is for a private version, and that’s where the built-in macro comes in (“the first character” etc). It is not editable.

This is the point in the code waiting for something to pop in:

template02

As you see, LoadAllOrders is not defined, and _order is not used. Positioning the cursor after the last method, as I start to type my, it finds myla:

template03

, finds the description, which is comforting that it has definitely found my template:

template04

Pressing tab gives this:

template05

Replace [ENTITY]  with [Order] (this has come from NorthWind) gives this:

template06

Press escape, and the editable references disappear. Note that it has done what it said re replacing the leading lower case. In fact, I thought I had done a typo, but it has just used a convention evidently to help it get there.

That principle can now be used for the Observable Collection property, for example, with the highlights being the candidates for replacement:

template07

This is how to get to the template editor, by the way:

template08

template09

Once you press the icon, the template editor appears but half-hides behind the templates explorer. So close the Templates Explorer (‘X’). Now you get a nice clean surface:

template10

The edit starts with the shortcut and description, and by pasting in the code we want to doctor:

template11Generally, we are replacing all instances of [order] with [entity]. This is the first part done:

template19

Next step is Choose macro for the ENTITY variable, that is, the one we are replacing at paste-time. Click OK once you’ve done the highlighted parts:

template20

Then define the LC_ENTITY variable, which derives from the ENTITY variable. Click OK once that is done :

template21

You now have this:

template22

A quirk I’ve noticed is that it seems there is no obvious button to save the template. So just CTRL-S, I suggest, and when you reopen the Templates Explorer, your template should be there:

template23

Let us now see if it actually works. Deleting the existing property-pair brings up errors:

template17

Start to type oc… and it finds your template. Accept that, and you get this:

template24

Then follow usual snippet replacement actions. Now in my case, I’ve made a mistake, as you can from this:

template25

I need to go back and look at how I defined LC_ENTITY, and my red Order should have been pluralised. In fact I see that LC_ENTITY did not get replaced because it was still set at editable, so make sure you set it to Not Editable. Secondly, if you look at the screenshots above, although I set LC_ENTITY to the macro to be based on another variable, I didn’t say what that variable is (ie ENTITY). So correct that.

Once corrected in the template, this is the generated version:

template26

And this is the final template:


private ObservableCollection _$LC_ENTITY$s;
public ObservableCollection $ENTITY$s {
get { return _$LC_ENTITY$s ?? (_$LC_ENTITY$s = new ObservableCollection(_ctx.$ENTITY$s)); }
private set {
if (_$LC_ENTITY$s == value) return;
_$LC_ENTITY$s = value;
RaisePropertyChanged(() => $ENTITY$s);
}
}

And the previous template…

private void LoadAll$ENTITY$s() {
_ctx.$ENTITY$s.Clear();
_$LC_ENTITY$ = _ctx.Get$ENTITY$sQuery();
_ctx.Load(_$LC_ENTITY$, LoadAll$ENTITY$sCompleted, null);
}

private void LoadAll$ENTITY$sCompleted(LoadOperation op) {
Add$ENTITY$SetToCollection();
}

private void Add$ENTITY$SetToCollection() {
foreach (var $LC_ENTITY$ in _ctx.$ENTITY$s) {
$ENTITY$s.Add($LC_ENTITY$);
}
}

And to remind myself to come up with some technique for columns…


public OrderDetailViewModel() {
if (DesignerProperties.IsInDesignTool) {
var list = new List
{
new Order {ShipAddress = "Tottenham", ShipCity = "Lisbon", ShippedDate = new DateTime(2012, 3, 3, 12, 12, 12)},
new Order {ShipAddress = "Liverpool", ShipCity = "Moscow"}
};
Orders = new ObservableCollection(list);
} else {
_ctx = new NWDomainContext();
LoadAllOrders();
}
}

And as I’m making it REALLY untidy, another point to self…

template27Finall

Finally, and again, quite tangentially, a cheatsheet for xaml:

A few days later… In an attempt to to continue to eat my own dog, here is just the text of a the Property, the delegate, the CanSubmit method, and the Submit, as it was then.


using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;

private ICommand _submitCommand;
public ICommand SubmitCommand {
get { return _submitCommand; }
set { if (_submitCommand == value) return;
_submitCommand = value;
RaisePropertyChanged(() => SubmitCommand);
}
}

// constructor begin – this version assumes a string parameter
SubmitCommand = new DelegateCommand(Submit, CanSubmit);
// constructor end

public bool CanSubmit(string cmdParameter) {
if (cmdParameter == null) return true;
return true; //add a condition here… or unconditional true
}

private static void Submit(string cmdParameter) {
// Action to be executed on Submit – this only gets called
// if CanSubmit returned true
}

// Example binding for the xaml
// <Button Content=”Button” HorizontalAlignment=”Left” Height=”36″ Margin=”120,201,0,0″ VerticalAlignment=”Top” Width=”111″ Command=”{Binding SubmitCommand}” CommandParameter=”{Binding Text, ElementName=MyBox}” />

What we have below is the standard public property with a private backing field. Yes, JetBrains have given us a perfectly good one of those as a template, but doesn’t this give you a feeling of more control?

Templates001

TemplatesB02

TemplatesB03

The reason for the commented-out parts is so that if this is the first time for a property, then the Using is already there. The XAML snippet is there so I can bind without thinking at all about how to write it – it might not be the final thing, but it’s probably a good 80/20.

private $VarType$ _$LC_propname$;
public $VarType$ $UC_propname$ {
get { return _$LC_propname$; }
set { if (_$LC_propname$ == value) return;
_$LC_propname$ = value;
RaisePropertyChanged(() => $UC_propname$);
}
}

plus…


// using Microsoft.Practices.Prism.Commands;
// using Microsoft.Practices.Prism.ViewModel;

private ICommand _$type$Command;
public ICommand $UC_type$Command {
get { return _$type$Command; }
set { if (_$type$Command == value) return;
_$type$Command = value;
RaisePropertyChanged(() => $UC_type$Command);
}
}

// constructor begin – this version assumes a string parameter
$UC_type$Command = new DelegateCommand($UC_type$, Can$UC_type$);
// constructor end

private bool Can$UC_type$($generic_type$ cmdParameter) {
// Add logic here. If true is returned, then $UC_type$ is called.
// Remove the unconditional return, as required.
return true;
}

private static void $UC_type$($generic_type$ cmdParameter) {
// Action to be executed on $UC_type$ – this only gets called
// if Can$UC_type$ returned true
}

private $VarType$ _$LC_propname$;
public $VarType$ $UC_propname$ {
get { return _$LC_propname$; }
set { if (_$LC_propname$ == value) return;
_$LC_propname$ = value;
RaisePropertyChanged(() => $UC_propname$);
}
}
TemplatesB06

TemplatesB07

ROKU Streaming Stick

stick01

I just bought one of these for my wife, who has does not want Sky, but is frustrated at the lack of programmes on Terrestrial TV. There is of course CatchUp of various kinds, but she doesn’t want a laptop hooked up to the TV, nor a PS3.

So it felt that Catchup is the way to go, and given her requirements, Chromecast or Roku were the choices. The remote on the Roku is much preferable to an app on a phone as far as she is concerned, so I bought that, specifically the Streaming Stick version. I was still torn, but this review from Stuff Magazine sold it in the end.

Right now, she is delighted, as the service is good, and the remote is intuitive. We’ll review that tonight as the contention rates increase.