Skip to main content

Posts

Showing posts with the label C#

Refactoring an interface to facilitate unit testing

Short post to follow, generalising something I've just done in an active project that seems to have general relevance. I had an interface and class like this, that wasn't easy to put under a unit test. interface ISomething { string GetValue(); string GetComplicatedConvertedValue(); } class MySomething : ISomething { string GetValue() { // Get value from something that's a simple one-liner call but not easy to mock or stub (in my case, Sitecore settings) ... } string GetComplicatedConvertedValue() { var rawValue = GetValue(); // Do some processing on the raw value that ideally we'd like under unit test, and return it ... } } One answer I realised is that, as the main value for testing this class is in the second method - retrieving the raw value is hard to test, but little value as it's a one liner into platform functionality - I can just remove that from the interface, and...

ASP.Net 5 and C# 6: Slides and Resources

ASP.Net 5 and C# 6: Slides and Resources Had the pleasure of speaking yesterday at the London Umbraco meet-up hosted at Zone on the topic of ASP.Net 5 and C# 6. For anyone who was there and wants a reference, or who wasn't and is interested in an introduction to this area, I'll post a few resources. Firstly the slides from the talk are available here . And then here's a number of links to blogs and other online resources that I've used in researching the topic and preparing the talk: Intro to ASP.Net 5 from Scott Gu Video introduction from Scott Hanselman and Scott Hunter Source code repository ASP.Net 5 documentation ASP.NET 5 Middleware, Or Where Has My HttpModule Gone? Understanding ASP.NET 5 middleware Taking Control of Configuration in MVC6 Introduction to view components Introduction to tag helpers Creating a custom tag helper C# FAQs - New Features in C# 6 New Language Features in C# 6 MSDN magazine: The New and Improved C# 6.0 You can e...

Code As Data With Reflection

Other than occasional use, working with reflection using C# isn't something I've had that much need to do. Recently though I was working on an additional feature for the Umbraco package The Dashboard . It provides a nice first screen for editors, showing their recent edits and other work carried out on the site content. The idea for the new feature was to create a developer version of this, that displayed various details about the code that had been implemented on the project. The use case being in the case of inheriting or coming back to a site, being able to have a quick overview of the techniques that have been used for route hijacking, surface controllers, application events and the like. In order to implement this it was going to be necessary to make quite heavy use of reflection and this post highlights a few of the methods used. Getting instances of types In the case of Umbraco route hijacking using custom controllers it's necessary to implement an MVC con...

Async and await with external web resources

Async and await are two keywords that have been available in the C# for a while now. And so whilst probably a bit late to the party thought would share how I've used them for a recent feature, in case they are also new to others. They allow you to write async code much more easily than was previously possible, which should allow applications to scale better - particularly when there's a need to call out to slow running processes (e.g. external web services), or really anything that is "IO bound" (like a database call. So for example you could speed up a web request that requires two calls to two long running processes by running them in parallel on different threads and then waiting for both to return. If they both take 1 second your result can return in 1 second instead of 2. But even with just one long running process in the request, it's still useful, as whilst that process is running the thread serving the request is no longer blocked, and can return ...

Working with Stylecop

I was recently asked to review the use of Stylecop as part of our standard procedures for ASP.Net projects. I'd not used it before, but was fairly happy to consider doing so as code styling has always been one of my pedantic points when reviewing code (including my own!). Have to admit I was less keen once I'd installed it and found it complaining with around several hundred warnings - on a single, not that complex, and - I'd thought - beautifully coded class library. Having uninstalled it, calmed down a bit a couple of weeks later, reinstalled it, and put aside an afternoon to work through one particular solution... came to the conclusion it did have quite a lot of value. Although no individual change was of any great significance, the code certainly felt better once the refactoring effort had been completed - and we'd expect over time that any best practices it enforced we weren't adhering too, would become normal coding style. So our plan is to use it on...

C# Language Extensions to Support LINQ

On the final day of Devweek this year I attended a full-day workshop on LINQ. It was initially billed as a day of “Entity Framework and LINQ”, but annoying the first bit of the title was dropped at some point before the day! Nonetheless it was a good presentation of what LINQ is, what language extension have been developed to support it, and how it is used in LINQ to Objects, LINQ to XML and LINQ to Entities. The talk was given by Niels Berglund. In order to support LINQ, a number of amends and enhancements were made to the .Net compiler and the VB.Net and C# languages – I found it worth getting my head around these, so that the LINQ language introduced later appeared less like “magic”. Automatic Properties Traditionally properties would be created in a class with a public accessor and a private backing store: private string mstrFirstName; public string FirstName { get { return mstrFirstName; } set { mstrFirstName = value; } } At the compiled code level this hasn’t changed, bu...