locked
How to call repository to do CRUD RRS feed

  • Question

  • User828542847 posted

    see the below code i got from this url https://msdn.microsoft.com/en-us/library/dn314429%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396

    using System.Collections.Generic; 
    using System.Data.Entity; 
     
    namespace TestingDemo 
    { 
        public class BloggingContext : DbContext 
        { 
            public virtual DbSet<Blog> Blogs { get; set; } 
            public virtual DbSet<Post> Posts { get; set; } 
        } 
     
        public class Blog 
        { 
            public int BlogId { get; set; } 
            public string Name { get; set; } 
            public string Url { get; set; } 
     
            public virtual List<Post> Posts { get; set; } 
        } 
     
        public class Post 
        { 
            public int PostId { get; set; } 
            public string Title { get; set; } 
            public string Content { get; set; } 
     
            public int BlogId { get; set; } 
            public virtual Blog Blog { get; set; } 
        } 
    }
    
    
    public class BlogService 
        { 
            private BloggingContext _context; 
     
            public BlogService(BloggingContext context) 
            { 
                _context = context; 
            } 
     
            public Blog AddBlog(string name, string url) 
            { 
                var blog = _context.Blogs.Add(new Blog { Name = name, Url = url }); 
                _context.SaveChanges(); 
     
                return blog; 
            } 
     
            public List<Blog> GetAllBlogs() 
            { 
                var query = from b in _context.Blogs 
                            orderby b.Name 
                            select b; 
     
                return query.ToList(); 
            } 
     
            public async Task<List<Blog>> GetAllBlogsAsync() 
            { 
                var query = from b in _context.Blogs 
                            orderby b.Name 
                            select b; 
     
                return await query.ToListAsync(); 
            } 
        } 

    my question is how can i call BlogService class various method to perform crud operation. anyone show me how to use it.

    thanks

    Tuesday, July 4, 2017 11:41 AM

Answers

  • User-271186128 posted

    Hi tapan2017,

    How to call repository to do CRUD

    From your description, it seems that you want to use Repository Pattern, I suggest you could refer to the following code:

    Creating the Repository class:

        public interface IStudentRepository : IDisposable
        {
            IEnumerable<Student> GetStudents();
            Student GetStudentByID(int studentId);
            void InsertStudent(Student student);
            void DeleteStudent(int studentID);
            void UpdateStudent(Student student);
            void Save();
        }
       public class StudentRepository : IStudentRepository, IDisposable
        {
            private SchoolContext context;
    
            public StudentRepository(SchoolContext context)
            {
                this.context = context;
            }
    
            public IEnumerable<Student> GetStudents()
            {
                return context.Students.ToList();
            }
    
            public Student GetStudentByID(int id)
            {
                return context.Students.Find(id);
            }
    
            public void InsertStudent(Student student)
            {
                context.Students.Add(student);
            }
    
            public void DeleteStudent(int studentID)
            {
                Student student = context.Students.Find(studentID);
                context.Students.Remove(student);
            }
    
            public void UpdateStudent(Student student)
            {
                context.Entry(student).State = EntityState.Modified;
            }
    
            public void Save()
            {
                context.SaveChanges();
            }
    
            private bool disposed = false;
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        context.Dispose();
                    }
                }
                this.disposed = true;
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }

    Then, you could use the following code to instantiate the repository class:

         private IStudentRepository studentRepository;
    
          public StudentController()
          {
             this.studentRepository = new StudentRepository(new SchoolContext());
          }
    
          public StudentController(IStudentRepository studentRepository)
          {
             this.studentRepository = studentRepository;
          }

    and use the following code to call the relevant method:

    Student student = studentRepository.GetStudentByID(id);

    More details, see: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    https://www.codeproject.com/Articles/615499/Models-POCO-Entity-Framework-and-Data-Patterns

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 5, 2017 3:00 AM

All replies

  • User2119946224 posted

    Hi,

    As per your code it's simply allow you to create an object inside your controller/page.

    var Obj= New BlogService(Pass Context object here);

    Wednesday, July 5, 2017 12:37 AM
  • User-271186128 posted

    Hi tapan2017,

    How to call repository to do CRUD

    From your description, it seems that you want to use Repository Pattern, I suggest you could refer to the following code:

    Creating the Repository class:

        public interface IStudentRepository : IDisposable
        {
            IEnumerable<Student> GetStudents();
            Student GetStudentByID(int studentId);
            void InsertStudent(Student student);
            void DeleteStudent(int studentID);
            void UpdateStudent(Student student);
            void Save();
        }
       public class StudentRepository : IStudentRepository, IDisposable
        {
            private SchoolContext context;
    
            public StudentRepository(SchoolContext context)
            {
                this.context = context;
            }
    
            public IEnumerable<Student> GetStudents()
            {
                return context.Students.ToList();
            }
    
            public Student GetStudentByID(int id)
            {
                return context.Students.Find(id);
            }
    
            public void InsertStudent(Student student)
            {
                context.Students.Add(student);
            }
    
            public void DeleteStudent(int studentID)
            {
                Student student = context.Students.Find(studentID);
                context.Students.Remove(student);
            }
    
            public void UpdateStudent(Student student)
            {
                context.Entry(student).State = EntityState.Modified;
            }
    
            public void Save()
            {
                context.SaveChanges();
            }
    
            private bool disposed = false;
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        context.Dispose();
                    }
                }
                this.disposed = true;
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }

    Then, you could use the following code to instantiate the repository class:

         private IStudentRepository studentRepository;
    
          public StudentController()
          {
             this.studentRepository = new StudentRepository(new SchoolContext());
          }
    
          public StudentController(IStudentRepository studentRepository)
          {
             this.studentRepository = studentRepository;
          }

    and use the following code to call the relevant method:

    Student student = studentRepository.GetStudentByID(id);

    More details, see: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    https://www.codeproject.com/Articles/615499/Models-POCO-Entity-Framework-and-Data-Patterns

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 5, 2017 3:00 AM
  • User828542847 posted

    thanks for your nice details answer.

    public StudentController(IStudentRepository studentRepository)
    {
        this.studentRepository = studentRepository;
    }

    tell me when this controller will be called ?

    show me how to call the above controller before to get student details.

    thanks

    Wednesday, July 5, 2017 12:00 PM