tags:

views:

51

answers:

2

could some body explain me abstraction and interface in asp.net, C# by taking an apprpriate example...pleasse i am not understanding it for long

+3  A: 

I often find the following sample quite illuminating when it comes to explaining this:

Disclaimer: the code examples are written directly into the text, and may contain errors that I have overseen. Please let me know if you find such errors.

Let's say you have a database with a Customer table, and in your code you have a customer class:

class Customer
{
   public int Id { get; set; }
   public string Name { get; set; }
}

In order to provide a mechanism to get customer data from the database, you need to write some class doing that. This can be placed in something called a repository. Now, we don't want our code to rely too much on what exact database we use. It could be SQL server, it could be a text file. So we want an abstraction layer shielding the code from this knowledge. All we need to know is what such a repository looks like:

public interface ICustomerRepository
{
    Customer GetCustomer(int id);
    IEnumerable<Customer> FindCustomers(string beginningOfName);
}

We can now implement this interface for the data storage that we use:

public class SqlServerCustomerRepository : ICustomerRepository
{
    public Customer GetCustomer(int id)
    {
        using(SqlConnection connection = new SqlConnection(connectionString))
        {
            // code to fetch data and populate Customer objects go here
        }
    }
    // implementations of other members of ICustomerRepository
    // left out to keep code short. Just imagine they are here :)
}

Finally, when we want to use this code, we can have a factory create the concrete ICustomerRepository implementation to use:

public static class RepositoryFactory
{
    public static ICustomerRepository CreateCustomerRepository()
    {
        return new SqlServerCustomerRepository();
    }
}

...and in our code where we need the data:

ICustomerRepository repository = RepositoryFactory.CreateCustomerRepository();
IEnumerable<Customer> customers = repository.FindCustomers("A");

This way, there is no hard coupling between the consuming code, and the particular kind of repository in use (except for in the factory method, but that is the one and only place where this knowledge exists). This makes it easy to replace the concrete repository implementation. This is also useful for testing, where you can easily create a mock repository returning hard coded results for given input, so that you can unit test the code that needs data from the repository.

Fredrik Mörk
sir what is IEnumerable here, seems difficult to understand me please elaborate a mre on this..i am novice
NoviceToDotNet
@NovicetoDotNet: `IEnumerable<T>` is an interface that is implemented by virtually all list types (including arrays) in .NET. Basically what it does is that it allows you to loop over the items it contains using a `foreach` loop, and it is also the type around which a lot of LINQ functionality is built.
Fredrik Mörk
+1  A: 

Well abstract classes and interfaces are not strictly asp.net technic they are OOP concept.

Interface

An interface is like a class but all the methods and properties are abstract. An Interface cannot be instantiated like abstract class. All the methods and properties defined in Interface are by default public and abstract.

Interface generally refers to an abstraction that an entity provides of itself to the outside. Interface can help in separating the methods for external and internal communication without effecting in the way external entities interact with the type.. Example: If you have interface IDoSomething { void Do(); } The class that implements the interface must provide a body for Do() method e.g.

class SomeClass : IDoSomething { public void Do() { //body of the method } } The advantage of this is when you make something that need only Do method you pass the interface not the class.

public static void SomeMethod(IDoSomething obj) { obj.Do(); } Now SomeMethod(IDoSomething obj) will work with any class that implements IDoSomething

Abstract Class

An abstract class is a class with at least one method defined as abstract. This type of class cannot be instantiated. An abstract class can have one or more abstract methods and properties and other methods and properties like normal classes.

The idea is the same but in abstract class you can have methods with implemented logic, fields and so on.

ikirachen