Last active
January 23, 2016 00:48
-
-
Save nbxx/c59e2f81ef6c5c5e5362 to your computer and use it in GitHub Desktop.
YouCanHackIt.ArchitectureDesign.DI.Samples.SetterInjection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace YouCanHackIt.ArchitectureDesign.DI.Samples | |
{ | |
public class SetterInjection | |
{ | |
public void Run() | |
{ | |
ICalculator calculator1 = new DefaultTaxCalculator(); | |
ICalculator calculator2 = new FoodTaxCalculator(); | |
ICalculator calculator3 = new BookTaxCalculator(); | |
TaxClient taxClient = new TaxClient(); | |
taxClient.Calculator = calculator1; | |
var tax1 = taxClient.GetTax(10m); //tax1=0.9 | |
taxClient.Calculator = calculator2; | |
var tax2 = taxClient.GetTax(10m); //tax2=0 | |
taxClient.Calculator = calculator3; | |
var tax3 = taxClient.GetTax(10m); //tax3=0.1 | |
} | |
public interface ICalculator | |
{ | |
decimal Calculate(decimal value); | |
} | |
public class DefaultTaxCalculator : ICalculator | |
{ | |
public decimal Calculate(decimal value) | |
{ | |
return value * 0.09m; | |
} | |
} | |
public class FoodTaxCalculator : ICalculator | |
{ | |
public decimal Calculate(decimal value) | |
{ | |
return value * 0m; | |
} | |
} | |
public class BookTaxCalculator : ICalculator | |
{ | |
public decimal Calculate(decimal value) | |
{ | |
return value * 0.01m; | |
} | |
} | |
public class TaxClient | |
{ | |
public ICalculator Calculator { get; set; } | |
public decimal GetTax(decimal price) | |
{ | |
return this.Calculator.Calculate(price); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment