20

This is a relatively straight forward question. But I was wondering what the correct usage is for accessing a method inside a separate project through the use of an interface.

Project: Test.ClassLibrary

Interface:

public interface ITest
{
    string TestMethod();
}

Class:

public class Test : ITest
{
    public string TestMethod()
    {
        return "Test";
    }
}

Project: Test.Web

Controller:

public class HomeController : Controller
{
    private ITest test;
    public ActionResult Index()
    {
        return Content(test.TestMethod());
    }

}

The above returns a NullReferenceException. I'm assuming it's because the controller gets to the interface, and doesn't know where to go next.

What's the best way to fix this? Do I have to reference the Test class in the controller or can I some how get away with only having a reference to ITest?

6
  • Do you actually have an instance of ITest into your test variable? If test == null, you'll get that error, and it doesn't have anything related to your ITest interface Commented Oct 14, 2011 at 1:49
  • Edited the question: I forgot to write : ITest with the Test class declaration Commented Oct 14, 2011 at 2:01
  • possible duplicate of What is a NullReferenceException in .NET? Commented Oct 14, 2011 at 2:13
  • This has nothing to do with interfaces. Commented Oct 14, 2011 at 2:13
  • How does it not? I'm asking how to use access a method through an interace without referencing the class that holds that method Commented Oct 14, 2011 at 2:20

4 Answers 4

27
  • You never instantiate ITest test, you only declare it.
  • Your Test class doesn't inherit from the interface.

You need to update your class declaration

public class Test : ITest // interface inheritance 
{

And in your controller, instantiate test.

ITest test = new Test();

As you get further along, you'll want to explore techniques for injecting the Test instance into the controller so that you do not have a hard dependency upon it, but just on the interface ITest. A comment mentions IoC, or Inversion of Control, but you should look into various Dependency Inversion techniques techniques (IoC is one of them, dependency injection, etc).

Sign up to request clarification or add additional context in comments.

4 Comments

Or instead of instantiating Test you could use an IoC Container to resolve the concrete class for ITest.
This works, but it still depends on the Test class. Can you elaborate on the other techniques if they avoid this?
@rudeovskizebear, I'll be honest and say that the ASP.NET MVC model is not something I use, but googling MVC + Dependency Injection returns a great many results, and apparently they made it easier in ASP.NET MVC 3. Perhaps a good place to start is this blog series.
Thanks for your help. You got me on the right path. I found Unity.Mvc3 for easy dependency: devtrends.co.uk/blog/…
5

The class needs to read:

public class Test : ITest

in its declaration.

3 Comments

Interface methods do not need to be declared abstract. In fact, they cannot have any modifiers.
The code is missing some sort of object creation (new, IoC container, factory, etc..) for Test()
Yes, I missed that on my initial reading. Need to initialize the object like so: private Test test = new Test()
4

First off, you need to have your Test class inherit/implement ITest.

class Test : ITest
{
    public string TestMethod() { return "test"; }
}

Then, in your controller class, you need to initialize test -- whether directly, or in the constructor.

public class HomeController : Controller
{
    public ITest test = new Test();
    public ActionResult Index()
    {
        return Content(test.TestMethod());
    }
}

Although in many cases, you should prefer to create the ITest outside of the constructor and pass it in or something.

Comments

2

What ?

Interfaces are basically a contract that all the classes implementing the Interface should follow. They looks like a class but has no implementation.

In C# Interface names by convention is defined by Prefixing an 'I' so if you want to have an interface called shapes, you would declare it as IShapes

Now Why ?

Improves code re-usability

Lets say you want to draw Circle, Triangle. You can group them together and call them Shapesand have methods to draw Circle and Triangle But having concrete implementation would be a bad idea because tomorrow you might decide to have 2 more Shapes Rectangle & Square. Now when you add them there is a great chance that you might break other parts of your code.

With Interface you isolate the different implementation from the Contract


How to use them ?

Usage Scenario Day 1

You were asked to create an App to Draw Circle and Triangle

interface IShapes
{
   void DrawShape();
}

class Circle : IShapes
{
    
    public void DrawShape()
    {
        Console.WriteLine("Implementation to Draw a Circle");
    }
}

class Triangle : IShapes
{
    public void DrawShape()
    {
        Console.WriteLine("Implementation to draw a Triangle");
    }
}
static void Main()
{
     List <IShapes> shapes = new List<IShapes>();
     shapes.Add(new Circle());
     shapes.Add(new Triangle());

     foreach(var shape in shapes)
     {
         shape.DrawShape();
     }
}

Usage Scenario Day 2

If you were asked add Square and Rectangle to it, all you have to do is create the implentation for it in class Square: IShapes and in Main add to list shapes.Add(new Square());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.