Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some advantages to using an interface in C#?

Tags:

I was forced into a software project at work a few years ago, and was forced to learn C# quickly. My programming background is weak (Classic ASP).

I've learned quite a bit over the years, but due to the forced nature of how I learned C#, there are a lot of basic concepts I am unclear on.

Specifically, an interface. I understand the basics, but when writing an app, I'm having a hard time figuring out a practical use of one. Why would one want to write an interface for their application?

Thanks Kevin

like image 950
Kevin Avatar asked Jun 23 '09 22:06

Kevin


People also ask

What are the advantages of using interfaces in C#?

One of the major advantages of Interface in C# is a better alternative to implement multiple inheritances. The interface enables the plug-and-play method. Complete Abstraction can be achieved by the implementation of Interface. Along with making our code easy to maintain, concept loose coupling can be achieved.

What is an advantage of using interfaces in your class design?

Using an interface is far more powerful than implementing one method in multiple classes, because if each class implements the same interface, you can treat them all the same (without casting).


1 Answers

An interface says how something should work. Think of it as a contract or a template. It is key to things such as Inverson of Control or Dependancy Injection.

I use Structure Map as my IoC container. This allows me to define an interface for all of my classes. Where you might say

Widget w = new Widget(); 

I would say

IWidget w = ObjectFactory.GetInstance<IWidget>(); 

This is very powerful in that my code isn't saying necessarily what a Widget truely is. It just knows what a Widget can do based on the interface of IWidget.

This has some great power to it in that now that I am using an IoC container I can do a couple more nifty things. In my unit tests where I need to use a Widget I can create a mock for Widget. So say that my Widget does something very powerful by way of connecting to a database or a web service, my mock can simulate connecting to these resources and return to me stubbed data. This makes my test run faster and behave in a way that is more reliable. Because I am using StructureMap I can tell StructureMap to load the real implementation of my Widget during production use of my code and the mocked version of the Widget during testing either programatically or by configuration.

Also, because I am using an IoC container I can provide cool new features to my application such as writing three different ways to cache data. I can have a local developer box cache using a tool such as Lucene.NET for a local cache. I can have a development server use the .NET cache which runs great on one box. And then I can have a third option for my production servers use a cache layer such as MemCache Win32 or Velocity. As long as all three caching implementations conform to the same interface, their actual implementation doesn't concern me (or my code) at all. I simply ask StructureMap to go get the current environments implementation and then go to work.

If you follow Dependency Injection at all then interfaces come in handy here also with an IoC container such as StructureMap in that I can declare the usage of a class by way of an Interface in the constructor of my class.

public class Widget(IWidgetRepository repository, IWidgetService service) : IWidget {     //do something here using my repository and service } 

And then when I new up an instance of Widget by way of StructureMap such as this

IWidget widget = ObjectFactory.GetInstance<IWidget>(); 

Notice that I am not specifying the repository or service in the constructor. StructureMap knows by way of the interfaces specified in the constructor how to go get the appropriate instances and pass them in too. This makes very powerful and clean code!

All from the simple definition of Interfaces and some clever usage of them!

like image 189
Andrew Siemer Avatar answered Oct 12 '22 18:10

Andrew Siemer