locked
Repository pattern c# asp.net RRS feed

  • Question

  • User-2063567145 posted

    I need a brief good example of Repository pattern With entity framework in ASP.Net 4.5 .There a lot of examples on the internet im confused how to pick right one.
    Some Forks on internet are using Dependency Injection as well with repository pattern (for this they are using ninject, spring.net autofac etc etc) .
    No one is using Repository pattern with ASP.net 4.0/4.5 everyone is using it with WEB API/MVC.

    What are benefits of using Repository Pattern or when we should go for this pattern?

    Why we need ninject or other to implement DI?

    Wednesday, July 23, 2014 4:04 AM

Answers

  • User-821857111 posted

    In most of the MVC tutorials on this site (you can find them in the Learn section) you will see DbContext classes instantiated in controllers and EF queries made there. For example, you might see this:

    public class StudentController : Controller
    {
        private ContosoContext db = new ContosoContext();
    
        public ActionResult Index()
        {
            var students = db.Students.ToList();
            return View(students);
        }
    }

    Your UI knows nothing about EF. You can consider the controller part of the BLL in this example. You could refactor this so that the EF stuff is in a separate BLL Manager class:

    namespace BusinessLogic
    {
        public class StudentManger
        {
            private ContosoContext db = new ContosoContext();
    
            public List<Student> GetStudents()
            {
                return db.Students.ToList();
            }
        }
    }

    Now your controller would look like this:

    using BusinessLogicLayer;
    
    public class StudentController : Controller
    {
        private StudentManger studentManager = new StudentManager();
    
        public ActionResult Index()
        {   
            var students = studentManager.GetStudents();
            return View(students);
        }
    }

    You could add another layer between the BLL and EF to act as a repository wrapper so that your code would look like this:

    using BusinessLogicLayer;
    
    public class StudentController : Controller
    {
        private StudentManger studentManager = new StudentManager();
    
        public ActionResult Index()
        {   
            var students = studentManager.GetStudents();
            return View(students);
        }
    }
    
    using DataAccess;
    
    namespace BusinessLogic
    {
        public class StudentManger
        {
            private StudentRepository repo = new StudentRepository();
    
            public List<Student> GetStudents()
            {
                return repo.GetStudents();
            }
        }
    }
    
    
    namespace DataAccess
    {
        public class StudentRepository
        {
            private ContosoContext db = new ContosoContext();
    
            public List<Student> GetStudents()
            {
                return db.Students.ToList();
            }
        }
    }

    Best practice is to only apply these wrapper classes if you see a good reason for doing so in your application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 26, 2014 8:31 AM

All replies

  • User-821857111 posted

    Dependency Injection and the Repository Pattern are completely separate topics. You do not need to use one with the other.

    No one is using Repository pattern with ASP.net 4.0/4.5 everyone is using it with WEB API/MVC.

    If you are using Entity Framework with Web Forms, you are using the Repository pattern. A repository is a layer that transforms object oriented CRUD operations into database operations e.g. a GetPeople method call to a "Select * From People" SQL call. The repository also takes care of mapping the data from the database to domain objects and vice-versa.

    You would use the repository pattern whenever you want to deal with data in an object oriented way, rather than binding DataSets and DataReaders to server controls.

    Wednesday, July 23, 2014 5:17 AM
  • User-2063567145 posted

    if you are using Entity Framework with Web Forms, you are using the Repository pattern

    I'm using EF6 my problem is for each business object i have same methods like Create,Update,Delete etc and some entity specific methods as well, currently i'm repeating Create,Update,Delete method for each entity. e.g UserDB class ,CarDB class for both i have some similar method and some specific.

    Now link mentioned below provide some workaround to avoid such duplication

    This guy is using Repository with EF please see article and suggest me what is best Architecture/Design for such scenario

    http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle

    Thanks for your support

    Wednesday, July 23, 2014 7:08 AM
  • User-821857111 posted

    I'm using EF6 my problem is for each business object i have same methods like Create,Update,Delete etc and some entity specific methods as well,

    You already have that with the DbSet class in EF. There is no need to wrap context.Users.Add() in another layer such as UserDB.Add() (which simply calls context.Users.Add()) or even a generic version of Add.

    Now link mentioned below provide some workaround to avoid such duplication
    An easier way to avoid duplication is to avoid writing wrapper methods at all.

    Wednesday, July 23, 2014 8:13 AM
  • User-2063567145 posted

    Actually i want my Data Access Layer separate from UI ,i dont want to create mess in my UI, i want to place EF interaction in a separate layer. An simplest would be very helpfull for me.

    Saturday, July 26, 2014 2:13 AM
  • User-821857111 posted

    You shouldn't put calls to the Entity Framework in your UI in any case. You should put them in your Business Logic Layer.

    Saturday, July 26, 2014 4:00 AM
  • User-2063567145 posted

    Please suggest me some sample/example or any url supporting this. I have to know best practice of designing Business Logic Layer while using EF.

    Saturday, July 26, 2014 6:57 AM
  • User-821857111 posted

    In most of the MVC tutorials on this site (you can find them in the Learn section) you will see DbContext classes instantiated in controllers and EF queries made there. For example, you might see this:

    public class StudentController : Controller
    {
        private ContosoContext db = new ContosoContext();
    
        public ActionResult Index()
        {
            var students = db.Students.ToList();
            return View(students);
        }
    }

    Your UI knows nothing about EF. You can consider the controller part of the BLL in this example. You could refactor this so that the EF stuff is in a separate BLL Manager class:

    namespace BusinessLogic
    {
        public class StudentManger
        {
            private ContosoContext db = new ContosoContext();
    
            public List<Student> GetStudents()
            {
                return db.Students.ToList();
            }
        }
    }

    Now your controller would look like this:

    using BusinessLogicLayer;
    
    public class StudentController : Controller
    {
        private StudentManger studentManager = new StudentManager();
    
        public ActionResult Index()
        {   
            var students = studentManager.GetStudents();
            return View(students);
        }
    }

    You could add another layer between the BLL and EF to act as a repository wrapper so that your code would look like this:

    using BusinessLogicLayer;
    
    public class StudentController : Controller
    {
        private StudentManger studentManager = new StudentManager();
    
        public ActionResult Index()
        {   
            var students = studentManager.GetStudents();
            return View(students);
        }
    }
    
    using DataAccess;
    
    namespace BusinessLogic
    {
        public class StudentManger
        {
            private StudentRepository repo = new StudentRepository();
    
            public List<Student> GetStudents()
            {
                return repo.GetStudents();
            }
        }
    }
    
    
    namespace DataAccess
    {
        public class StudentRepository
        {
            private ContosoContext db = new ContosoContext();
    
            public List<Student> GetStudents()
            {
                return db.Students.ToList();
            }
        }
    }

    Best practice is to only apply these wrapper classes if you see a good reason for doing so in your application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 26, 2014 8:31 AM
  • User-1617232794 posted

    See generic Repository with entity framework

    http://ajaypatelfromsanthal.blogspot.in/2014/04/use-of-generic-with-repository-pattern.html

    Monday, July 28, 2014 6:29 AM