locked
Why should i use repository classes in asp.net mvc3? RRS feed

  • Question

  • What are the repository classes. Why should i use repository classes in asp.net mvc3?
    Monday, October 31, 2011 9:27 AM

Answers

  • Hi Nava,

    Repository classes are good because of two reasons:

    1.  They simplify querying and accessing your data by centralizing it inside of one object.  Now, instead of your controllers having to work directly with the Entity Framwork model directly and do Linq queries, all the controller has to do is make call to a method on your repository class.  The repository class centralizes all the logic and grunt work of getting data from the Model.

    2.  It aids in testing and doing dependency injection and unit tests.  By using a repository class, you can design it in such a way that it implements an interface for all its methods.  For example you might have an interface like this:

    public interface IMyRepository
    {
        List<Customer> GetAllCustomers();
        Customer GetCustomerByID(int _id);
        void UpdateCustomer(Customer _cust);
        void DeleteCustomer(Customer _cust);
        
    }
    
    

    Now, you can create as many repository classes that you need, that implement this interface.  They aid in testing because you can create a repository class that returns test data and is not linked to any database at all, which is important for proper unit testing.    If your controllers are set up to work with an any object that implements IMyRepository instead of working with an distinct class object, then it gives you flexibility when doing unit testing.  


    Tom Overton
    • Proposed as answer by Kris444 Tuesday, November 1, 2011 7:21 AM
    • Marked as answer by Alan_chen Thursday, December 29, 2011 6:26 AM
    Monday, October 31, 2011 12:49 PM
  • Hi

    Check with this

    http://channel9.msdn.com/Series/The-Full-Stack/The-Full-Stack-Part-3-Building-a-Repository-using-TDD

     

    http://channel9.msdn.com/search?term=Repository+pattern


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Alan_chen Thursday, December 29, 2011 6:26 AM
    Tuesday, November 1, 2011 10:28 AM
  • On 10/31/2011 5:27 AM, NavaKiran wrote:
    > What are the repository classes. Why should i use repository classes in
    > asp.net mvc3?
     
    Why use MVC at all? Model View Presenter (a derivative of MVC) is a much
    more flexible solution implementation than MVC.
     
    The shows in the link will show you an event driven MVP.
     
     
    You don't need to start it OnInit.
     
     
    You can implement it on the form's class constructor. I like the
    constructor better.
     
     Then it's just this.
     
    protected void Page_Load(object sender, System.EventArgs e)
    {
        presenter.PageLoad()
    }
     Private void btnAdd_Click(object sender,
    System.Web.UI.ImageClickEventArgs e)
    {
        presenter.btnAddClick();
         or
       presenter.btnAddClick(sender, e);  // if one needed to pass "sender"
    and "e" into the presenter's method.
     
    }
     
    • Marked as answer by Alan_chen Thursday, December 29, 2011 6:26 AM
    Tuesday, November 1, 2011 2:12 PM

All replies

  • Hi Nava,

    Repository classes are good because of two reasons:

    1.  They simplify querying and accessing your data by centralizing it inside of one object.  Now, instead of your controllers having to work directly with the Entity Framwork model directly and do Linq queries, all the controller has to do is make call to a method on your repository class.  The repository class centralizes all the logic and grunt work of getting data from the Model.

    2.  It aids in testing and doing dependency injection and unit tests.  By using a repository class, you can design it in such a way that it implements an interface for all its methods.  For example you might have an interface like this:

    public interface IMyRepository
    {
        List<Customer> GetAllCustomers();
        Customer GetCustomerByID(int _id);
        void UpdateCustomer(Customer _cust);
        void DeleteCustomer(Customer _cust);
        
    }
    
    

    Now, you can create as many repository classes that you need, that implement this interface.  They aid in testing because you can create a repository class that returns test data and is not linked to any database at all, which is important for proper unit testing.    If your controllers are set up to work with an any object that implements IMyRepository instead of working with an distinct class object, then it gives you flexibility when doing unit testing.  


    Tom Overton
    • Proposed as answer by Kris444 Tuesday, November 1, 2011 7:21 AM
    • Marked as answer by Alan_chen Thursday, December 29, 2011 6:26 AM
    Monday, October 31, 2011 12:49 PM
  • Hi Tom,

                 Thanks for your reply. I got some clarity. could you suggest me a tutorial for this?

    Tuesday, November 1, 2011 5:55 AM
  • Hi

    Also

    A long debate why we need repository at all, this is sort of duplicating and having extra layer above Entity Framework? 

    If your application is very simple this is right, Yes your application required only basic functionalities like Get, GetAll, Save...etc

    When you are working with Enterprise applications having Repository layer is needful in my opinion

    1> Makes easy for TDD as Tom mentioned

    2> What if you have to right query for some scenario? it is better to be all these in Repository layer rather than upper layer..

    3> EntityFramework does not provide event listeners and Interceptors as NHibernate do, So this could be the place if you wanted to implement...

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Tuesday, November 1, 2011 7:20 AM
  • Hi

    Check with this

    http://channel9.msdn.com/Series/The-Full-Stack/The-Full-Stack-Part-3-Building-a-Repository-using-TDD

     

    http://channel9.msdn.com/search?term=Repository+pattern


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Alan_chen Thursday, December 29, 2011 6:26 AM
    Tuesday, November 1, 2011 10:28 AM
  • On 10/31/2011 5:27 AM, NavaKiran wrote:
    > What are the repository classes. Why should i use repository classes in
    > asp.net mvc3?
     
    Why use MVC at all? Model View Presenter (a derivative of MVC) is a much
    more flexible solution implementation than MVC.
     
    The shows in the link will show you an event driven MVP.
     
     
    You don't need to start it OnInit.
     
     
    You can implement it on the form's class constructor. I like the
    constructor better.
     
     Then it's just this.
     
    protected void Page_Load(object sender, System.EventArgs e)
    {
        presenter.PageLoad()
    }
     Private void btnAdd_Click(object sender,
    System.Web.UI.ImageClickEventArgs e)
    {
        presenter.btnAddClick();
         or
       presenter.btnAddClick(sender, e);  // if one needed to pass "sender"
    and "e" into the presenter's method.
     
    }
     
    • Marked as answer by Alan_chen Thursday, December 29, 2011 6:26 AM
    Tuesday, November 1, 2011 2:12 PM