We design our apps with design principles and best practices. The resulting apps usually have dependency injection in place already. We then can use Ninject as the dependency injection tool, and set up in-memory test objects along with our production codes.

The question is that how can we click a button to swap the in-memory test environment and debug/release environment easily?

Luckily, there are  two features in Visual Studio, build configuration and conditional compilation, which can solve our problem easily.

  1. In VS solution, create a new build configuration, InMemory, and choose to inherit the setting from Debug.
  2. Add a constant ‘INMEMORY’ to the ‘Conditional compilation symbols’ in the ‘Build’ tab of project property.
  3. Add conditional compilation in Ninject’s Register Services code block

 

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name=”kernel”>The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
#if INMEMORY
kernel.Bind<IYourInterface>().To<YourInMemoryImplementation>();
#else
kernel.Bind<IYourInterface>().To<YourDebugReleaseImplementation>();
#endif
}

Advertisements