Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:34:45+00:00 2026-06-04T00:34:45+00:00

I have read from internet I got this points which says Interfaces is used

  • 0

I have read from internet I got this points which says Interfaces is used for this

  • Use TDD methods
  • Replace persistance engine

But I’m not able to understand how interface will be usefull to this point Replace persistance engine.
lets consider I’m creating a basic(without generics) repository for EmployeeRepository

public class EmployeeRepository
{
  public employee[] GetAll()
  {
     //here I'll return from dbContext or ObjectContex class
  }
}

So how interfaces come into picture?

and if suppose i created an interface why upcasting is used ? for e.g

 IEmployee emp = new EmployeeRepository() ;
 vs
 EmployeeRepository emp = new EmployeeRepository();

Please explain me precisely and also other usefullness of Interface in regard to Repository Pattern.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:34:47+00:00Added an answer on June 4, 2026 at 12:34 am

    So how interfaces come into picture ?

    Like this:

    public interface IEmployeeRepository
    {
        Employee[] GetAll();
    }
    

    and then you could have as many implementations as you like:

    public class EmployeeRepositoryEF: IEmployeeRepository
    {
        public Employee[] GetAll()
        {
            //here you will return employees after querying your EF DbContext
        }
    }
    
    public class EmployeeRepositoryXML: IEmployeeRepository
    {
        public Employee[] GetAll()
        {
            //here you will return employees after querying an XML file
        }
    }
    
    public class EmployeeRepositoryWCF: IEmployeeRepository
    {
        public Employee[] GetAll()
        {
            //here you will return employees after querying some remote WCF service
        }
    }
    
    and so on ... you could have as many implementation as you like
    

    As you can see it’s not really important how we implement the repository. What’s important is that all repositories and implementations respect the defined contract (interface) and all posses a GetAll method returning a list of employees.

    And then you will have a controller which uses this interface.

    public class EmployeesController: Controller
    {
        private readonly IEmployeeRepository _repository;
        public EmployeesController(IEmployeeRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index()
        {
            var employees = _repository.GetAll();
            return View(employees);
        }   
    }
    

    See how the controller no longer depends on a specific implementation of the repository? All it needs to know is that this implementation respects the contract. Now all that you need to do is to configure your favorite dependency injection framework to use the implementation you wish.

    Here’s an example of how this is done with Ninject:

    1. Install the Ninject.MVC3 NuGet
    2. In the generated ~/App_Start/NinjectWebCommon.cs code you simply decide to use the EF implementation with a single line of code:

      private static void RegisterServices(IKernel kernel)
      {
          kernel.Bind<IEmployeeRepository>().To<EmployeeRepositoryEF>();
      }        
      

    This way you no longer need to do any manual instantiations of those repository classes and worry about upcasting or whatever. It is the dependency injection framework that manages them for you and will take care of injecting the defined implementation into the controller constructor.

    And by simply modifying this configuration you could switch your data access technology without touching a single line of code in your controller. That’s way unit testing in isolation also comes into play. Since your controller code is now weakly coupled to the repository (thanks to the interface we introduced) all you need to do in the unit test is to provide some mock implementation on the repository which allows you to define its behavior. This gives you the possibility to unit test the Index controller action without any dependency on a database or whatever. Complete isolation.

    I also invite you to checkout the following articles about TDD and DI in ASP.NET MVC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read from this article http://codahale.com/how-to-safely-store-a-password/ and it says using a salt isn't
I read about JSON from internet but still i have not got the grasp
I have read from some article that say's Apple doesn't approve the application which
I have read this article from High Scalability about Stack Overflow and other large
I have a string read from another source such as \b\bfoo\bx. In this case,
I have read from the internet that unless you work with old version sql
I have read from the question How to gain access to a ServletContext instance
Problem: We have to read from a proprietary binary file at work. It changes
I have read about session fixation and from what I understand it forces a
I have some code to read from a pdf file. Is there a way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.