2

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

Community
  • 1
  • 1
sakir
  • 3,228
  • 8
  • 29
  • 50

2 Answers2

3

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
}
Karl Anderson
  • 33,426
  • 12
  • 62
  • 78
  • where should I store all interfaces inside project,inside the DAL? or bll or a different class. thank you again – sakir Sep 19 '13 at 15:01
  • @user2460637 - I store my interfaces in the domain layer, which all layers of my application have a reference to. I recommend that approach, because if you put the interfaces in the DAL, then any layer that needs these interfaces will need a reference to the DAL. It is generally a bad practice to have, potentially, all layers dependent upon the DAL. Ideally, you would have a service layer that knew about the domain objects in your system and the interfaces of how to store and retrieve those objects. Just my two cents, use it however you wish. :-) – Karl Anderson Sep 19 '13 at 15:07
  • @user2460637 Check out Domain Driven Design (DDD). Here's an article you can start with: http://msdn.microsoft.com/en-us/magazine/dd419654.aspx – Garrison Neely Sep 19 '13 at 15:20
  • I was going to asking you ,"what do u mean the domain layer " ,was it another name of BLL or were we are talking about the domain layer"??but it seems totally different implemantation from the "typed DataSet" which I gave the link my question. – sakir Sep 19 '13 at 15:25
  • what if I wanna apply n-tire architecture,based on the link I gave on my question,what is your personel experiance about the implemening interface.do u apply them inside BLL? thank you again – sakir Sep 19 '13 at 15:31
  • I have not used the typed DataSet approach much at all, but you could use the interfaces in your BLL. – Karl Anderson Sep 19 '13 at 16:54
2

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
    }
}
Garrison Neely
  • 3,310
  • 3
  • 24
  • 39
  • thank you garrison,it is so clear,I will search the key words and concepts u gave. – sakir Sep 19 '13 at 14:51
  • garrison ,where u store your interface ,do u use a DDD as Karl did,or do u have another approch ,thank you again – sakir Sep 19 '13 at 15:26
  • 1
    For the larger projects, DDD is the way to go, because it can get complex very quickly. Even for an n-tier application, I'd probably incorporate a Domain layer that is the root that all other layers reference. You'd have Interfaces, any Data Transfer Objects (objects used to send data between layers), Models in the Domain layer. Then BLL and DAL would reference the Domain layer. DAL would implement Interfaces and use DTOs to return formatted data to the BLL. This would allow you to not have BLL referencing the DAL, which helps keep things loosely coupled. – Garrison Neely Sep 19 '13 at 15:40