0

This is an aspnetcore class library project I have an interface

public interface ITest
{
    string TestMethod(string name);
}

and a class that implements the interface

public class Test : ITest 
{
    public virtual string TestMethod(string name)
    {
        return "hello " + name;
    }
}

to prove that this works, I have a console application where I make a simple call

using TestProject.Test;  

public class Tester
{
    public void TestMethod()
    {
        var testClass = new Test();
        Console.WriteLine(testClass.TestMethod("John")); // Print "hello John"
    }
}

Now, I created a new project, also from aspnetcore class library, I imported the first project, and I want to overwrite the method

using TestProject.Test; 

public class TestOverwritten : Test 
{
    public override string TestMethod(string name)
    {
        return "(overwritten) hello " + name;
    }
}

My question is: how can I make the call to this overwritten method, without having to modify the call from the console application? it's possible? If so, is there a concept that defines what I want to do? (will it be IoC, or maybe encapsulation?)

using TestProject.Test;  

public class Tester
{
    public void TestMethod()
    {
        var testClass = new Test(); // I assume this line should be different ... but how?
        Console.WriteLine(testClass.TestMethod("John")); // This should print "(overwritten) hello John"
    }
}

when I say "without having to modify the call from the console application" I mean that, the creation of the object "var testClass = new Test ();" be generic enough to not need to be modified, but still recognize that you should no longer seek the implementation of the method to the class "Test", but to the class "TestOverwritten". Perhaps through a configuration file that reads the classes that implement the interface? that way you could modify the configuration file (from "Test" to "TestOverwritten"), and not have to modify all the methods that invoke the "Test" class.

  • `new TestOverwritten()`? – Johnathan Barclay Jan 25 '20 at 01:04
  • 1
    fyi... it's overridden, not overwritten... these things matter, esp in job interviews. "overriding" and "overwriting" are both important concepts... but they are completely unrelated to each other. – JoelFan Jan 25 '20 at 01:10
  • @JohnathanBarclay I added a more specific detail at the end of the question – Isaias Peña Cromilakis Jan 25 '20 at 01:13
  • 2
    The interface is superfluous detail here. You're not using it. You could remove it and it wouldn't affect the code in the question one bit. – madreflection Jan 25 '20 at 01:15
  • Have a look at this: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1 – Johnathan Barclay Jan 25 '20 at 01:18
  • @madreflection exact! and that is what I want, I want the console application to use the interface to make the call, but without knowing what class actually implements it, I will know at the time of the call only, for that I need to have the possibility to indicate what class implement the interface somewhere (configuration file? how?) – Isaias Peña Cromilakis Jan 25 '20 at 01:18
  • You could do that with an interface, but you could also do that without it. As JohnathanBarclay pointed out, `Test testClass = new TestOverridden();` (<-- terminology corrected, BTW.) You have a reference to the base class and you don't care which derived class's implementation you actually have. This is polymorphism, very basic OOP stuff. Learn interfaces *after* you learn polymorphism with simple class hierarchies. Also, make sure you get the terminology right -- "override/overridden", not "overwrite/overwritten" -- you'll get better search results. – madreflection Jan 25 '20 at 01:22
  • I understand and appreciate the advice, so far, the closest thing to what I want to do is this: https://stackoverflow.com/questions/13130637/unity-inject-different-classes-for-the-same-interface – Isaias Peña Cromilakis Jan 25 '20 at 01:43
  • Have a look at the [factory method pattern](https://en.wikipedia.org/wiki/Factory_method_pattern). – gdir Jan 25 '20 at 02:24

1 Answers1

0

Actually you can not, you must make the call to the derived class.

 var testClass = new TestOverwritten();

so the compiler knows which one you want to use the parent or the derived.

  • Are you sure you can't? What I am trying to do is that the console application is able to make a call to a method through the interface definition. At runtime, that "resolution" would determine through a configuration file, which is the class that implements the methods of that interface (in this case, you should call the "TestMethod" method of the overwritten class). I've been looking for a lot of information ... the closest thing I've found has been using Unity, but I can't implement it the way I need because I don't have all the knowledge – Isaias Peña Cromilakis Jan 25 '20 at 01:40
  • like this https://stackoverflow.com/questions/13130637/unity-inject-different-classes-for-the-same-interface – Isaias Peña Cromilakis Jan 25 '20 at 01:42
  • In order to *"make a call to a method through the interface definition"*, you need to declare the variable as the interface type. Avoid using `var` for now. It's convenient but it will hide important details, like the fact that the variable is actually of type `Test`, not `ITest`. – madreflection Jan 25 '20 at 02:26