Phoney


Yesterday I wrote six lines of code.  Seven if you count the connection string. These are them:

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));


// Create the queue client.
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();


// Retrieve a reference to a container.
CloudQueue queue = queueClient.GetQueueReference("formsservicequeue");


// Create the queue if it doesn't already exist
queue.CreateIfNotExists();


// Create a message and add it to the queue.
CloudQueueMessage message = new CloudQueueMessage(urlEncodedXmlFormData);

queue.AddMessage(message);

I say wrote, what I actually did was copy and paste from the MSDN documentation and make a small change.

The rest of the day was spent figuring out which particular combination of NuGet package and Azure Storage Emulator would work with the old code this was added to and how to add unit tests around it.

In the end I gave up on unit tests and just did some manual integration testing as the original code never had unit test written, is integrated with Dynamics CRM 4.0 Organisation, Discovery and Metadata services, is itself an old SOAP service and is scheduled for replacement in the next few months.

The amount of work required to separate the components, write fakes for all the services and add in dependency injection is just not worth the benefit in this particular case.

Advertisements

Adding style to [Required] fields in MVC with Razor


So you have a nice little model decorated with Attributes, and you’ve templated out your views but the users don’t know which fields are mandatory, the horror!

Here’s a model example:

public class PreQualificationModel
{
    
    [Display(Name = "Post code")]
    [Required]        
    public string PostCode { get; set; }

    [Display(Name = "Number of employees")]
    [Required]
    public float NumEmployees { get; set; }

    [Display(Name = "Balance sheet (in GBP)")] 
    [DisplayFormat(DataFormatString = "{0:C}")]
    [Required]
    public decimal BalanceSheetGBP { get; set; }

    [Display(Name = "Sales turnover (in GBP)")]
    [DisplayFormat(DataFormatString = "{0:C}")]
    [Required]
    public decimal SalesTurnoverGBP { get; set; }
}

 

Usually you would have some indicator on the UI to show which fields are mandatory, a little red asterisk for example.  You could go through your view and add them manually but what happens when the model changes?  You have to do it all again, yuk.

but what if you could edit the EditorFor (and displayFor) templates to include it for you?  well you can, here is a simple example:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,

new { @class = "text-box single-line", type = "number" }) @if (ViewData.ModelMetadata.IsRequired) { <b style="color:Red;">*</b> }

Just create a folder called Editor Templates under Views/Shared in your solution and save this as String.cshtml and suddenly all string fields get red asterisks if they are mandatory.  you can do a similar thing for Decimal and other types as necessary.