Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I confused about interfaces?

Tags:

c#

interface

I am maintaining an ASP.NET MVC project. In the project the original developer has an absolute ton of interfaces. For example: IOrderService, IPaymentService, IEmailService, IResourceService. The thing I am confused about is each of these is only implemented by a single class. In other words:

OrderService : IOrderService
PaymentService : IPaymentService

My understanding of interfaces has always been that they are used to create an architecture in which components can be interchanged easily. Something like:

Square : IShape
Circle : IShape

Furthermore, I don't understand how these are being created and used. Here is the OrderService:

public class OrderService : IOrderService
{
    private readonly ICommunicationService _communicationService;
    private readonly ILogger _logger;
    private readonly IRepository<Product> _productRepository;

    public OrderService(ICommunicationService communicationService, ILogger logger,
        IRepository<Product> productRepository)
    {
        _communicationService = communicationService;
        _logger = logger;
        _productRepository = productRepository;
    }
}

These objects don't seem be ever be created directly as in OrderService orderService = new OrderService() it is always using the interface. I don't understand why the interfaces are being used instead of the class implementing the interface, or how that even works. Is there something major that I am missing about interfaces that my google skills aren't uncovering?

like image 332
Nick Avatar asked Dec 09 '11 14:12

Nick


People also ask

Are interfaces overused?

Interfaces are overused in a lot of C# code, and in some cases are not merely extra baggage, but are actively the wrong thing to do. These interfaces tend to be created upfront, with the object, in advance of any need of them; and are wide, mirroring all the public parts of the object.

Do I need an interface for every class?

When discussing these principles in the book, I regularly encourage the reader to add more interfaces to their classes, to make the overall design of the package or application more flexible. However, not every class needs an interface, and not every interface makes sense.

What is the default access modifier of the members of an interface can I declare any of the members of an interface Public explicitly why?

As suggested by others, you can not have a access modifier (or virtual) in an interface member. By default (and by definition) all members inside an Interface are public and abstract.


2 Answers

This particular design pattern is typically to facilitate unit testing, as you can now replace OrderService with a TestOrderService, both of which are only referenced as IOrderService. This means you can write TestOrderService to provide specific behavior to a class under test, then sense whether the class under test is doing the correct things.

In practice, the above is often accomplished by using a Mocking framework, so that you don't actually hand-code a TestOrderService, but rather use a more concise syntax to describe how it should behave for a typical test, then have the mocking framework dynamically generate an implementation for you.

As for why you never see 'new OrderService' in the code, it's likely that your project is using some form of Inversion of Control container, which facilitates automatic Dependency Injection. In other words, you don't have to construct OrderService directly, because somewhere you've configured that any use of IOrderService should automatically be fulfilled by constructing a singleton OrderService and passing it in to the constructor. There are a lot of subtleties here and I'm not exactly sure how your dependency injection is being accomplished (it doesn't have to be automatic; you can also just construct the instances manually and pass them in through the constructors.)

like image 144
Dan Bryant Avatar answered Nov 03 '22 20:11

Dan Bryant


That's not the only use of interfaces, in MVC they are being used to decouple contract from implementation. To understand about MVC you need to read up a bit on the related topics such as separation of concerns and inversion of control (IoC).The actual act of creating an object to be passed to OrderService constructor is handled by IoC container based on some predefined mapping.

like image 45
mmix Avatar answered Nov 03 '22 18:11

mmix