Dependency injection (misunderstanding?)
# help-with-other
r
Hey there! I'm trying to understand more about DI and testing out some abstraction with a class, a base class and the interface. Given the attached, I would have expected the output to be "Super" when called in the program, but it's coming out as "Base" instead. I'm thinking this would be a nice solution to implement a caching layer of some of my services; so in this example
TestMethod
would be the service getting the raw data, then
SuperTestMethod
would act as a wrapper to implement the cache. I feel as though I'm doing something silly to miss this! Can someone please help me understand? https://cdn.discordapp.com/attachments/1285985075779080203/1285985076076871681/image.png?ex=66ec426e&is=66eaf0ee&hm=985f33bc630f9482f97303794d567d6b425af7a58ec8290359eec28e846ced7e&
l
This actually isn't a DI issue, but an inheritance issue. You've declared a
new
GetValue()
method. You didn't override the existing one. To make the existing one overridable, mark it as
virtual
on the base class, then exchange your
new
keyword in the deriving class for
override
.
r
Thanks for that @Licht ! It put me on the right path and sorted out my issue 🙌 I ran a small console app to validate the thinking and if I'm correct it's the 4th object that mimics what DI is doing (so ITestMethod istm = new SuperTestMethod()). Time to hit the books and read some more. https://cdn.discordapp.com/attachments/1285985075779080203/1286076097217171466/image.png?ex=66ec9733&is=66eb45b3&hm=9514b2bad6fe0ecb013f4253e3bf0497b8db70613fe6a370b2441d16087749d6& https://cdn.discordapp.com/attachments/1285985075779080203/1286076097493991435/image.png?ex=66ec9733&is=66eb45b3&hm=df789f19493151377c224924c8e3e023949c2e8d46d6e96ce46c9ee9ca0e2bdf&
l
Happy coding buddy.