Wednesday, 24 March 2010

DevWeek 2010

Just got back from DevWeek 2010.  Here is a list of things I need to follow up on from the sessions I attended:

A Day of ASP.NET 4.0 : Fritz Onion

  • MemCache
  • Chart Control
  • GridView/ListView ‘remembers’ selected
  • Query Extender
  • Html Encode syntax and HtmlString class
  • AJAX 4.0
    • Script Loader/Manager
    • Sys.Require
    • Sys.onReady
    • CDN
    • Extender Controls.js
    • Client Templates {{propName}}
    • .sys-template class
    • Sys.create.dataView(“#id”, { data : data })
    • sys: prefix on attributes
    • Sys.bind(Sys.Get(“selector”))
  • Routing in ASP.NET Forms
    • RouteValue, RouteUrl, RouteParameter
  • Web Application rather than Web Site
  • Deployment:
    • Package/Publish, XDT in config files
    • IIS Import on farm
  • .svc services /js to get client proxy
    • [DataContract] [DataMember]
    • live binding {binding Title}
    • Sys.Observer.makeObservable(itemsArray);
    • Sys.Data.DataContext
  • ADO.NET Dynamic Data

97 Things Every Programmer Should Know : Kevlin Henney

  • Bought the book

Objects of Desire : Kevlin Henney

  • Parameter Objects
  • Pass in Context
  • Interpreter / Aggregator Patterns
  • Getter/Setter problem
  • Keep object valid at all times
  • Privatize
  • LSP
  • UML Inheritance Hierarchies
  • Layering: Concept <– Realisation
  • Layering: Domain –> Services –> Infrastructure
  • Apathy, ignorance, selfish
  • Names based on client usage not implementation

VSTS 101 – a beginner’s guide : Brian Randell

  • Project Collection (single db) > Team Project
  • Static Code Analysis
  • Code Metrics
  • Check-in policies
  • Team Build
  • Test impact analysis
  • VS Profiler?!
  • Mostly MS marketing

Agile DB techniques using VS 2010 : Giles Davies

  • Build Screen Saver, Brian the build bunny
  • Extension Properties for schema version number
  • Data Transfer Plan, Data Generation Plan
    • Data bound generator
  • Got to use this!

Intro to Sharepoint 2010 from ASP.NET devs : Fritz Onion

  • VS 2010 makes it easier.  Needs huge buy in.

Entity Framework in .NET 4 and VS 2010 : Eric Nelson

  • Entity Data Model (EDM)
    • Conceptual
    • Mapping
    • Storage
  • Lots of new goodness in 4.0
  • Insert Key
  • Foreign Key surfaced
  • ExecuteStoreQuery
  • EntityFunctions
  • Extension Manager
  • Feature Pack (POCO, code only, self-tracking)
  • Persistence Ignorance
  • http://bit.ly/ericnelson 

Code contracts and design for testability : Dino Esposito

  • Preconditions
  • Postconditions
  • Invariants
  • Interface contracts
  • Tools (rewriter, static checker, asm ref gen)
  • Contract Failure handling (in UT or logging)

Are singletons evil? : Andy Clymer & Kevin Jones

  • Singleton
  • Repository
  • Factory
  • Dependency Injection

Objects of value : Kevlin Henney

  • aka Attribute Object
  • No/insignificant identity
  • Create Value Objects that exist in the domain and ‘wrap’ simple types
  • Systems of Values
  • POVO
  • Patterns of Value : Immutable Value, Copied Value
  • Three finger rule: Identity, State, Behaviour
  • Smells: Anaemia and Multiple Stereotypes
  • Anatomy: Construction, Comparison, Classification and Conversion
  • Domain specific
  • Equals
  • My MonetaryAmount example

Programming with GUTs : Kevlin Henney

  • Things not to test in UT (perhaps in Integration tests)
  • POUT, TDD, DDT
  • Test per proposition not per method
  • Naming:
    • proposition not implementation
    • Underscores
  • Example based
    • Simple cases
    • Common cases
    • Boundary cases
    • Contractual error cases (rainy day)
  • Better roughly right than precisely wrong
  • Black box

Patterns dartboard : Andy Clymer & Kevin Jones

  • Adapter / Proxy
  • Null
  • Active Record
  • Strategy

Is the free lunch over? : Andy Clymer

  • Fork/Join (Parallel.Invoke, Tasks)
  • Pipeline (Queues, production line, stages)
  • Geometric decomposition (Break down the data)
  • Use Sequential version as UT?
  • Parallel Loops (loop of loops, Barrier)
  • Monte Carlo (educated guesses, best result in time T)

Use threads effectively to build responsive & scalable software : Jeffery Richter

  • Threads are bad
  • Threads are good
  • Async Programming Model (APM)
  • ThreadPool and Tasks
  • AsyncEnumerator

Friday, 14 August 2009

T-SQL Cursor Template

This is a handy cursor template for T-SQL on MS SQL Server:
DECLARE curName CURSOR LOCAL FAST_FORWARD
FOR
select *
from mytablename

DECLARE variables

OPEN curName
WHILE 1=1
BEGIN
FETCH NEXT FROM curName
INTO variables

IF @@fetch_status <> 0
BEGIN
BREAK
END

--do work

END

CLOSE curName
DEALLOCATE curName

Friday, 17 July 2009

Writing an event log entry from an ASP.NET application

By default the ASPNET account under which ASP.NET applications cannot write to the registry. So if you are writing an event log entry with a new source it will fail.

The simple answer is to run regedit and go to:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application

Then create a new key with the name of your event source.

Full details can be found here.

Wednesday, 14 January 2009

Using an ASP.NET handler to output a CSV file

I wanted a ASP.NET handler to output some data as a CSV file with the most likely target being to open it in Excel.

I had the following problems to resolve:

  1. Force download rather than opening in IE.

  2. Numbers beginning with zeros (0) must not be stripped off in Excel.

  3. Creating the CSV from an XML file using XSLT.

  4. Decoding entities (e.g. &amp;) in XSLT.

  5. Accessing Session State from the handler code.
So lets look at these in order ...

1. Force download rather than opening in IE.

This one is fairly simple to resolve with some addition of headers:

context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", "inline; filename=\"file.csv\"");

The only thing to look out for here is to use Response.AddHeader rather than the Response.Headers collection as it won't work on most IIS platforms.

2. Numbers beginning with zeros (0) must not be stripped off in Excel.

This solution is a disappointing but effective one. Enclose the number in quotes with a leading equals sign.

So 01234 becomes ="001234"

This is a very Excel bias solution but was good enough for me. Another solution is to lead with a single quote but unfortunately this will initially show in the Excel cell until the user 'edits' the cell then it disappears.

3. Creating the CSV from an XML file using XSLT.

This is fairly straightforward, here's the XSLT:

<xsl:stylesheet version="1.0" xsl="http://www.w3.org/1999/XSL/Transform" msxsl="urn:schemas-microsoft-com:xslt" prefixes="msxsl">
<xsl:output method="text" encoding="utf-8">

<xsl:template match="doc">
<xsl:apply-templates select="row">
</xsl:apply-templates>

<xsl:template match="row">
<xsl:for-each select="*">
<xsl:value-of select="." disable-output-escaping="yes" />
<xsl:if test="position() != last()">
<xsl:value-of select="','" />
</xsl:if>
</xsl:for-each>
<xsl:text>&#10;</xsl:text>
</xsl:template>
</xsl:stylesheet>

This assumes an Xml document hierarchy of doc > row > fields.

This is fine except that often you want to output only certain fields, perhaps in a particular format, and show the field headings in the first row. The above can be adapted for this by swapping out the "for-each" for a "value-of" naming each field individually.

4. Decoding entities (e.g. &amp;) in XSLT.

In the above Xslt you will notice that I have added disable-output-escaping which will do just that.

5. Accessing Session State from the handler code.

The class definition for a handler is by default:

public class CsvHandler : IHttpHandler

To indicate that you need session state too you need to add another interface although this is just a marker (probably should have been an attribute), I only need read access so:

public class CsvHandler : IHttpHandler, IReadOnlySessionState


Okay that covers all the issues listed above.

Visual Studio keyboard layout swaps from UK to US

Solution: Hotkey is Shift+Alt

I've been having problems in Visual Studio where my keyboard would suddenly swap from UK to US layout. From that point on the " key would map to @ and vice versa.

I eventually got round to googling the problem and came to this Microsoft connect item: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=269947

So I learn that there is a hot key - Shift+Alt - that does this and so the problem is reversable. But I have to agree with the person who sent in that item. Why does this hot key exist or perhaps why is it such a simple combo? The only thing I can think of is that MS people testing VS wanted this. To me it is just a pain.

Monday, 17 November 2008

Clean Code by Robert C. Martin Review (Ch.1)

I recently bought the new book by one of my favourite software development writers, Robert C. Martin (Uncle Bob). I consider myself an experienced software developer and perhaps should already know how to write "Clean Code". Hopefully this book will give me confirmation that I'm on the right track but also I hope to learn a thing or three.

This book is Java programming orientated but the principles should easily transferred to any other programming language. Since my programming language of choice is C# I decided I'd give the book a review and translation to my way of thinking in C#.

Chapter 1: "Clean Code" is the argument for the existence of a book about writing clean code and why it is important. These arguments are well put and well documented in other literature. In a nutshell, code is read more often than it is written and this is as true on new projects as it is in maintenance tasks.

I have no problem with this, the problem comes in the definition of what "Clean Code" looks like. Uncle Bob saves the day by not claiming that this book is the be all and end all definition of what clean code should be. He simply says that the book represents the current opinion of Uncle Bob and his compatriots. Therefore it is open to your modification and, possibly, improvement.

I like that. It gives me the opportunity to think out how I may apply this to my own work without necessarily getting too dogmatic.

As I read the book I'll post my review and C# translations here.