Question

My question is related to the bll,dal,interfaces.

My project's constructure more or less like this. BLL, DAL, OBJ and 3 layer architecture (As I dont repeat question and codes again,I give the link here)

My question is why should I use interfaces,what is the benefits.and how can apply interfaces based on the project structure which is gaved above. could u provide link or answers.thank you all

Was it helpful?

Solution

Interfaces allow you to define the behavior without the actual implementation, think of it as a contract.

If you only have one implementation, then an interface is not very useful and is not recommended.

Where interfaces shine, is when you have multiple implementations of the same logic. Say for example the data access layer (DAL), like this:

public interface IPersonRepository
{
    Person CreatePerson(string firstName, string lastName, int age);
    Person LoadPerson(int personId);
    Person SavePerson(string firstName, string lastName, int age);
    bool DeletePreson(int personId);
}

Now if you have a SQL Server database, then you could have a repository class that implements the IPersonRepository interface, like this:

public class SqlServerPersonRepository : IPersonRepository
{
    // Implement SQL Server specific logic here
}

Let's say you want to support Oracle, as well, then you create an OraclePersonRepository, like this:

public class OraclePersonRepository : IPersonRepository
{
    // Implement Oracle specific logic here
}

What is also useful is that you can create a mock person repository (for testing), like this:

public class MockPersonRepository : IPersonRepository
{
    // Implement mock logic here
}

OTHER TIPS

Interfaces are useful in a lot of examples. To give you one of the most popular, consider the Repository pattern commonly used for Data Layer implementation.

Let's say I implement my DAL for SQL Server. In the future, my company decides to switch to MySQL. All of my BLL calls to the DAL are now vulnerable to being rewritten/dramatically modified.

If I had used an interface (say IRepository), I could have written SqlRepository, which implements IRepository. I would then have the BLL reference IRepository, using Dependency Injection to give SqlRepository to the BLL at runtime. When the business decided to use MySQL, I can then write MySqlRepository, implement IRepository, and then all of my BLL doesn't have to be rewritten to handle MySQL. In fact, my BLL doesn't even know SqlRepository or MySQLRepository exists. It just communicates via the interface IRepository.

Some other key uses for Interfaces are getting around the lack of multiple inheritance in C#, as well as for some Web Service implementations. I think for your current setup, the example I gave above is one of the more useful demonstrations of the usefulness and power of interfaces.

Definitely look up the Repository Pattern as well as Dependency Injection / Inversion of Control. Once you are comfortable with it, you'll find more and more places to use Interfaces to keep your code as loosely coupled as possible.

Here's a short example of the implementations of IRepository and SqlRepository:

public interface IRepository
{
    List<string> GetUserIds();

    void CreateUser(string userId);

    bool DeleteUser(string userId);
}

public class SqlRepository : IRepository
{
    public List<string> GetUserIds()
    {
        // Provide your implementation of GetUserIds.  
        // Connect to DB, retrieve data, return
    }

    public void CreateUser(string userId)
    {
        // Provide implementation
    }

    public bool DeleteUser(string userId)
    {
        // Provide implementation
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top