Entity Framework Repositories (Part 2)

This example tries to achieve the same result as the previous post, but with the cooperation of many dedicated repositories (one for each entity), in order to distribute the complexity.

The Car Repository (no dependencies)

public class CarRepository : IRepository<Car, int>
{
    private readonly DbContext _context;
 
    public CarRepository(DbContext context)
    {
        _context = context;
    }
 
    public IEnumerable<Car> GetAll()
    {
        return _context.Set<Car>().ToList();
    }
 
    public Car GetById(int id)
    {
        return _context.Set<Car>().Find(id);
    }
 
    public Car AddNew(Car entity, bool saveChanges = true)
    {
        return AddUpdate(entity, saveChanges);
    }
 
    public Car Update(Car entity, bool saveChanges = true)
    {
        return AddUpdate(entity, saveChanges);
    }
 
    public void Delete(int id, bool saveChanges = true)
    {
        var car = GetById(id);
        _context.Set<Car>().Remove(car);
        if (saveChanges)
            _context.SaveChanges();
    }
 
    public void SaveChanges()
    {
        _context.SaveChanges();
    }
 
    private Car AddUpdate(Car entity, bool saveChanges)
    {
        Car car;
        if (entity.IdCar == 0)
        {
            car = _context.Set<Car>().Create();
            _context.Set<Car>().Add(car);
        }
        else
            car = GetById(entity.IdCar);
        _context.Entry(car).CurrentValues.SetValues(entity);
        if (saveChanges)
            _context.SaveChanges();
        return car;
    }
}

The Person Repository (Car as dependency)

public class PersonRepository : IRepository<Person, int>
{
    private readonly DbContext _context;
    private readonly IRepository<Car, int> _carRepository;
 
    public PersonRepository(DbContext context,
        IRepository<Car, int> carRepository)
    {
        _context = context;
        _carRepository = carRepository;
    }
 
    public IEnumerable<Person> GetAll()
    {
        return _context.Set<Person>().ToList();
    }
 
    public Person GetById(int id)
    {
        return _context.Set<Person>().Find(id);
    }
 
    public Person AddNew(Person entity, bool saveChanges = true)
    {
        return AddUpdate(entity, saveChanges);
    }
 
    public Person Update(Person entity, bool saveChanges = true)
    {
        return AddUpdate(entity, saveChanges);
    }
 
    public void Delete(int id, bool saveChanges = true)
    {
        var person = GetById(id);
 
        //Delete dependencies before deleting
        person.Cars.ToList().ForEach(car =>
            _carRepository.Delete(car.IdCar, false));
        _context.Set<Person>().Remove(person);
        if (saveChanges)
            _context.SaveChanges();
    }
 
    public void SaveChanges()
    {
        _context.SaveChanges();
    }
 
    private Person AddUpdate(Person entity, bool saveChanges)
    {
        Person person;
        if (entity.IdCity == 0)
        {
            person = _context.Set<Person>().Create();
            _context.Set<Person>().Add(person);
        }
        else
            person = GetById(entity.IdPerson);
        _context.Entry(person).CurrentValues.SetValues(entity);
 
        //Delete dependencies before updating
        person.Cars.Where(car => entity.Cars.All(entityCar =>
            entityCar.IdCar != car.IdCar)).ToList().ForEach(car =>
                _carRepository.Delete(car.IdCar, false));
 
        //Add/Update dependecies before updating
        entity.Cars.ToList().ForEach(entityCar =>
        {
            //Ensures that the FK is set
            entityCar.IdPerson = entity.IdPerson;
            if (entityCar.IdCar == 0)
                person.Cars.Add(_carRepository.AddNew(entityCar, false));
            else
                _carRepository.Update(entityCar, false);
        });
        if (saveChanges)
            _context.SaveChanges();
        return person;
    }
}

The City Repository (Person as Dependency)

public class CityRepository2 : IRepository<City, int>
{
    private readonly DbContext _context;
    private readonly IRepository<Person, int> _personRepository;
 
    public CityRepository2(DbContext context,
        IRepository<Person, int> personRepository)
    {
        _context = context;
        _personRepository = personRepository;
    }
 
    public IEnumerable<City> GetAll()
    {
        return _context.Set<City>().ToList();
    }
 
    public City GetById(int id)
    {
        return _context.Set<City>().Find(id);
    }
 
    public City AddNew(City entity, bool saveChanges = true)
    {
        return AddUpdate(entity, saveChanges);
    }
 
    public City Update(City entity, bool saveChanges = true)
    {
        return AddUpdate(entity, saveChanges);
    }
 
    public void Delete(int id, bool saveChanges = true)
    {
        var city = GetById(id);
 
        //Delete dependencies before deleting
        city.Persons.ToList().ForEach(person =>
            _personRepository.Delete(person.IdPerson, false));
        _context.Set<City>().Remove(city);
        if (saveChanges)
            _context.SaveChanges();
    }
 
    public void SaveChanges()
    {
        _context.SaveChanges();
    }
 
    private City AddUpdate(City entity, bool saveChanges)
    {
        City city;
        if (entity.IdCity == 0)
        {
            city = _context.Set<City>().Create();
            _context.Set<City>().Add(city);
        }
        else
            city = GetById(entity.IdCity);
        _context.Entry(city).CurrentValues.SetValues(entity);
 
        //Delete dependencies before updating
        city.Persons.Where(person => entity.Persons.All(entityPerson =>
            entityPerson.IdPerson != person.IdPerson)).ToList().ForEach(person =>
                _personRepository.Delete(person.IdPerson, false));
 
        //Add/Update dependecies before updating
        entity.Persons.ToList().ForEach(entityPerson =>
        {
            //Ensures that the FK is set
            entityPerson.IdCity = entity.IdCity;
            if (entityPerson.IdPerson == 0)
                city.Persons.Add(_personRepository.AddNew(entityPerson, false));
            else
                _personRepository.Update(entityPerson, false);
        });
        if (saveChanges)
            _context.SaveChanges();
        return city;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s