Why Interface Layer/Abstract classes required in our project? [closed]

We normally use abstract function/Interfaces in our projects. Why it is really needed? Why can't we just go for Business logic Layer, Data Access Layer and Presentation Layer only

Function in Presentation Layer:

abc();

Function in Business Logic Layer:

 public void abc()
    {
      //Preparing the list
    }

Function in Data Access Layer:

public abstract void abc();

Function in Data Access SQLServer Layer:

 public override void abc()
    {
       //Connection with database
    }

Question is: Why is the Data Access Layer required ?


Solution 1:

The easiest way to understand this, imo, is an abstraction over DataLayer.

You have set a functions to retrieve a data from xml file. But one day your product scales out and xml is not enough like a data storage. So you pass to some embeded database: sqlite. But one day you need to reuse your library in some enterprise context. So now you need to develop access to sqlserver, oracle, webservice.... In all these changes you will need to change not only the code that actually access the data, but the code that actually consumes it too. And what about the code that already use for years your first xml data access on client and happy with it? How about backcompatibility?

Having the abstraction if not direcly solves most of this problems, but definitely makes scallable your application and more resistant to changes, that, in our world, happen sometimes too frequently.

Solution 2:

Generally, if you use interfaces in your code, then you will gain code manuverability in the form of Dependency Injection.

This will help you replace parts of your implementation in certain situations for example providing Mock objects during Unit Testing.

Solution 3:

Why interfaces : Have you ever used using in c# : using (Form f = new Form()) { }

Here you will see that you can use only those classes inside using which implements IDisposable interface .

Two things which does not know each other can interact with each other using Interfaces only. Interface gurantees that "some" functionality has surely been implemented by this type.

Why layers :

So that you can have separate dlls which will let you to reuse in different application.

Basically all is for code reuse and Performance gain.