• 1405/06/29

جواب تمرین 125-پیاده سازی الگویRepositoryبرای مدل Person :

سلام استاد 

پاسخ تمرین 125 که گفته بودید مدل جدولPerson رو به روشRepositori پیاده سازی کنیم :

من همون چند عملیاتی که در کلاس PersonManagement پیاده سازی کرده بودیم رو پیاده سازی کردم خدمتتون

interface IPersonRepository:

using DataLayer.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace DataLayer.Repositories.Contracts
{
    public interface IPersonRepository
    {
        List<Person> GetAllPerson();
        Person GetById(int id);
        List<Person> GetAllPersonsForSearch(string param);
        void Create(Person person);
        void Update(Person person);
        void Delete(Person person);
        void Delete(int id);
        void Save();

    }
}

PersonRepository:

using DataLayer.Context;
using DataLayer.Models;
using DataLayer.Repositories.Contracts;
using System;
using System.Collections.Generic;
using System.Text;

namespace DataLayer.Repositories.Repositories
{
    public class PersonRepository : IPersonRepository
    {
        TopLearn_DB_Context _context;
        public PersonRepository()
        {
            _context = new TopLearn_DB_Context();
        }
        public void Create(Person person)
        {
            _context.Persons.Add(person);
        }

        public void Delete(Person person)
        {
            _context.Persons.Remove(person);
        }

        public void Delete(int id)
        {
            var data = GetById(id);
            _context.Persons.Remove(data);
        }

        public List<Person> GetAllPerson()
        {
            return _context.Persons.ToList();
        }

        public Person GetById(int id)
        {
            var person = _context.Persons.Find(id);
            return person;
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        public void Update(Person person)
        {
            _context.Update(person);
        }

        public List<Person> GetAllPersonsForSearch(string param)
        {
            return _context.Persons.Where(p => p.Name.Contains(param) || p.Family.Contains(param)).ToList();
        }

        ~PersonRepository()
        {
            _context.Dispose();
        }
    }
}

ممنون از تدریس خوبتون استاد

🙏🙏

  • 1405/06/29
  • ساعت 08:59

سلام

بسیار عالی بود