Skip to content

Instantly share code, notes, and snippets.

@bernardobrezende
Created October 17, 2011 21:03
Show Gist options
  • Save bernardobrezende/1293809 to your computer and use it in GitHub Desktop.
Save bernardobrezende/1293809 to your computer and use it in GitHub Desktop.
SchoolOO's PersonDAO makeover
using System.Collections.Generic;
using System.Linq;
namespace SchoolOO.DAO
{
public class GrupoDePessoas
{
public IList<Person> PorOrdemAlfabetica()
{
using (SchoolEntities schoolContext = new SchoolEntities())
{
return schoolContext.People.OrderBy(x => x.FirstName).ToList<Person>();
}
}
public void Inserir(Person person)
{
using (SchoolEntities schoolContext = new SchoolEntities())
{
schoolContext.People.AddObject(person);
schoolContext.SaveChanges();
}
}
public void Atualizar(Person person)
{
using (SchoolEntities schoolContext = new SchoolEntities())
{
Person pessoaParaAlterar = schoolContext.People.Single(x => x.PersonID == person.PersonID);
pessoaParaAlterar.LastName = person.LastName;
pessoaParaAlterar.FirstName = person.FirstName;
pessoaParaAlterar.HireDate = person.HireDate;
pessoaParaAlterar.EnrollmentDate = person.EnrollmentDate;
schoolContext.SaveChanges();
}
}
public void Retirar(Person person)
{
using (SchoolEntities schoolContext = new SchoolEntities())
{
schoolContext.DeleteObject(person);
schoolContext.SaveChanges();
}
}
public void Retirar(int id)
{
using (SchoolEntities schoolContext = new SchoolEntities())
{
Person pessoaParaRetirar = schoolContext.People.First(x => x.PersonID == id);
// Opcional: chamar método acima passando o objeto da linha anterior.
schoolContext.DeleteObject(pessoaParaRetirar);
schoolContext.SaveChanges();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment