public HailstoneController(HailstoneNumberGenerator generator) { Generator = generator; }
/// <summary> /// This method illustrates method level dependency injection. /// </summary> /// <param name="generator">An instance of a HailstoneNumberGenerator.</param> /// <remarks> /// The method needs to do something, and it needs a HailstoneNumberGenerator to do it. /// /// The generator is provided to the method as a parameter. /// /// The parameter is specified as an interface so that the concrete instance of the type can change /// without the the method itself being changed. It also allows the method to be used/reused in more cases. /// /// For example /// DoStuff_Injected(new NaiveHailstoneNumberGenerator()); /// DoStuff_Injected(new OptimizedHailstoneNumberGenerator()); /// </remarks> public static void DoStuff_Injected(HailstoneNumberGenerator generator) { generator.ComputeHailstoneNumber(0); }