Program.cs
using AddKeyMethods;
using ConsoleApp1;
using Microsoft.Extensions.DependencyInjection;
var collection = new ServiceCollection();
collection.AddKeyedSingleton(Locations.India);
collection.AddKeyedSingleton(Locations.UAE);
collection.AddScoped();
collection.AddScoped();
collection.AddScoped();
var provider = collection.BuildServiceProvider();
//ITaxCalculator calculator = provider.GetKeyedService(Locations.India);
Console.Clear();
//Console.WriteLine($"Your tax rate is {calculator.CalculateTax()}");
//..........INDIA...............................
var purchase = provider.GetService();
var total = purchase?.CheckOut();
Console.WriteLine(total);
//..............................................
//..........UAE..............................
var uaePurchase = provider.GetService();
var totalUAEPrice = uaePurchase?.CheckOut();
Console.WriteLine(totalUAEPrice);
//..............................................
//.............MIX........
var mixPurchase = provider.GetService();
var totalMixPrice = mixPurchase?.CheckOut();
Console.WriteLine(totalMixPrice);
//.......................
Console.ReadKey();
ITaxCalculator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AddKeyMethods
{
public interface ITaxCalculator
{
int CalculateTax();
}
public class IndiaTaxCalculator : ITaxCalculator
{
public int CalculateTax() { return 30; }
}
public class UAETaxCalculator : ITaxCalculator
{
public int CalculateTax() { return 0; }
}
public enum Locations
{
India,
UAE,
USA
}
}
using AddKeyMethods;
using Microsoft.Extensions.DependencyInjection;
using System.Web.Http;
namespace ConsoleApp1
{
public class IndiaPurchase
{
private readonly ITaxCalculator _taxCalculator;
public IndiaPurchase([FromKeyedServices(Locations.India)]ITaxCalculator taxCalculator)
{
_taxCalculator = taxCalculator;
}
[HttpGet]
public int CheckOut()
{
var total = _taxCalculator.CalculateTax() + 200;
return total;
}
}
}
using AddKeyMethods;
using Microsoft.Extensions.DependencyInjection;
using System.Web.Http;
namespace ConsoleApp1
{
public class UAEPurchase
{
private readonly ITaxCalculator _taxCalculator;
public UAEPurchase([FromKeyedServices(Locations.UAE)]ITaxCalculator taxCalculator)
{
_taxCalculator = taxCalculator;
}
[HttpGet]
public int CheckOut()
{
var total = _taxCalculator.CalculateTax() + 0;
return total;
}
}
}
using AddKeyMethods;
using Microsoft.Extensions.DependencyInjection;
using System.Web.Http;
namespace ConsoleApp1
{
public class MixPurchase
{
private readonly ITaxCalculator _indtaxCalculator;
private readonly ITaxCalculator _uAEtaxCalculator;
public MixPurchase([FromKeyedServices(Locations.India)]ITaxCalculator indtaxCalculator, [FromKeyedServices(Locations.UAE)] ITaxCalculator uAEtaxCalculator)
{
_indtaxCalculator = indtaxCalculator;
_uAEtaxCalculator = uAEtaxCalculator;
}
[HttpGet]
public int CheckOut()
{
var total = _indtaxCalculator.CalculateTax() + 200 + _uAEtaxCalculator.CalculateTax() + 2;
return total;
}
}
}
OUTPUT
230
0
232
0
232