Created
October 17, 2011 21:03
-
-
Save bernardobrezende/1293809 to your computer and use it in GitHub Desktop.
SchoolOO's PersonDAO makeover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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