I'm knew in dependency injection, so I was testing it. I tested constructor injection in Console Application and it went good.
So I decide to test in AspNet Core. I realized it already had something built in for that, so I don't need to install and additional package.
So, my first code was running okay and I understood it what it happend.
This is my first code:
public interface ITea
{
string Flavour();
}
public class LemonTea : ITea
{
public string Flavour()
{
return "Lemon";
}
}
public class TamarindoTea : ITea
{
public string Flavour()
{
return "Tamarindo";
}
}
public class Tea
{
private ITea _tea;
//ctor injection
public Tea(ITea tea)
{
this._tea = tea;
}
public string GetFlavour()
{
return _tea.Flavour();
}
}
now in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddScoped<ITea, TamarindoTea>();
services.AddScoped<ITea, LemonTea>();
}
in HomeController
public IActionResult Index()
{
var tea = new Tea(_tea);
ViewBag.Test = tea.GetFlavour();
return View();
}
as a result I had
I've realized that the "type of flavour" I was having was according the last Added code in ConfigureServices.
So I decide to find I way to get a result similar to this code (I also wrote it)
In index...
ITea lemon = new LemonTea();
ITea tamarindo = new TamarindoTea();
var tea = new Tea(tamarindo);
ViewBag.Test = tea.GetFlavour();
The output here would depend on the type of ITea I inject in the constructor.
I don't know where I read, but something got me thinking that I was having too much dependencies in the second code
I was using ITea, Tea, LemonTea and TamarindoTea
So I excluded Tea, and added constructor dependency on the two other
public class LemonTea : ITea
{
private ITea _tea;
public LemonTea(ITea tea)
{
_tea = tea;
}
public string Flavour()
{
return "Lemon";
}
}
But for that to work, I would have to remove the code for ConfigureServices or else I had this error
Then I thought that something was wrong (that what I think), I mean can choose the type of tea at runtime, but I now I have to instatiate the class. It was the same as I do without caring about dependency injection (besides the fact that my class don't depend on other concrete classes)
Since I'm thinking that my code is wrong, I Google to how to inject different implementations in Aspnet core
I found this code from StackOverFlow and refactor it, I also had to refactor my code because of some errors
my refactor ended up like this:
public class LemonTea : ITea
{
public string Flavour()
{
return "Lemon";
}
}
public class TamarindoTea : ITea
{
public string Flavour()
{
return "Tamarindo";
}
}
--------------------Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddTransient<LemonTea>();
services.AddTransient<TamarindoTea>();
}
--------------------HomeController
private readonly IServiceProvider _serviceProvider;
public HomeController(ILogger<HomeController> logger, IServiceProvider serviceProvider)
{
_logger = logger;
_serviceProvider = serviceProvider;
}
public string GetTeaFlavour(string name)
{
if (name.ToLower() == nameof(LemonTea).ToLower())
return _serviceProvider.GetService<LemonTea>().Flavour();
else
return _serviceProvider.GetService<TamarindoTea>().Flavour();
}
public IActionResult Index()
{
ViewBag.Test = GetTeaFlavour(nameof(TamarindoTea));
}
I've archieved what I want, I just need to type the name of the class, and since I'm using nameof, I can refactor
So I was having some error so I've refactor my code in other to work.
In this case I didn't refactor because I knew what I was doing, I just knew how to find the solution. At some point I was understanding
what I was doing, then I got lost.
It's working, but I can't understand my code, I know how it works, but I don't know why. IFAIK that's very common for programmers
But I would like to have someone to explain to me what I've done and using my code (if possible, what would be the best implementation)