Showing posts with label IoC. Show all posts
Showing posts with label IoC. Show all posts

Wednesday, 31 July 2013

Navigating code which uses IoC

Recently I've been navigating around lots of code which uses interfaces to implement Inversion of Control.  The problem is that every time you use F12 to 'Go To Definition' you are taken to the interface, not any of the available implementations.  Most of the time there is only one real implementation, the other(s) are for unit tests.
 
Here are your quick navigation options:

 

Put interface and implementation in the same file

A low-tech option is just to put the main implementation of the interface in the same file as the interface itself.  In reality this is often not an option.

 

Call Hierarchy

You can use the built in 'Call Hierarchy' (Ctrl+k, Ctrl-t) to show implementations of the given function.
 

 

Productivity Power Tools

Productivity Power Tools (see here) allow you to click on the interface name and see 'Implemented By'.  You can then open the list of functions to get to the function you need.

 

Code Rush

There's a plugin for CodeRush here but I couldn't get it to work (even after I remembered to Unblock the downloaded DLL).  Also I found that CodeRush made my IDE grind to a halt (the solution is VERY large)

 

Visual Assist X

Visual Assist doesn't help (but also doesn't hinder any of the other options)

 

ReSharper

Apparently if you have ReSharper then ALT+END will find all implementors.
 
Note these hints are a summary of this post.
Visual Studio user voice suggestion that the functionality is added (here)

Wednesday, 24 April 2013

Design for testing

Thu 7th March Kevin Jones
Our goal is to produce flexible, maintainable, defect free code.
 
Code:
  • Automate the build and deploy process
  • Don't leak UI in to domain layers
  • Don't leak DB access across layers
  • Define new types for each concept even if they are small
    • e.g. Money type rather than using a double.
  • Build facades
    • Amalgamate behaviour in to a new type
    • Hide complexity of underlying objects
  • Single responsibility principle
  • Using concrete types is often a bad idea
    • Code to interfaces
    • Can replace real objects with test objects
    • Can use Dependency Injection
    • Use of 'new' means we are tied to an implementation
Test:
  • Tests should be isolated
  • Unit tests shouldn't use system (file / DB etc.)
  • Test names should be descriptive (use the BDD style)
    • Given_When_Than()
    • e.g. GivenAUserService_WhenIAskForAllUsers_ThenAllUsersAreReturned()
  • Arrange -> Act -> Assert
Dependency Injection
  • Parameter injection
  • Setter injection
  • Constructor injection
  • Use an IoC container (e.g. Unity / Castle / Ninject / AutoFac for MVC)
  • IoC (Inversion of Control) is a principal, Dependency Injection is a consequence of IoC