Repository pattern Andres Kver IT Kolled 20162017 Spring






![using Contacts. Library; namespace Repo. Console. App { class Program { static void Main(string[] using Contacts. Library; namespace Repo. Console. App { class Program { static void Main(string[]](https://slidetodoc.com/presentation_image_h/0cf8ee09aade24b9b368e68345b0c641/image-7.jpg)






- Slides: 13
Repository pattern Andres Käver, IT Kolledž 2016/2017 Spring
public interface IPerson. Repository : IDisposable { IQueryable<Person> All { get; } IQueryable<Person> All. Including( params Expression<Func<Person, object>>[] include. Properties); Person Find(int id); void Insert. Or. Update(Person person); void Delete(int id); void Save(); }
public void Insert. Or. Update(Person person) { if (person. Person. ID == default(int)) { // New entity context. People. Add(person); } else { // Existing entity context. Entry(person). State = Entity. State. Modified; } } public class Person. Repository : IPerson. Repository { Contact. Context context = new Contact. Context(); public IQueryable<Person> All { get { return context. People; } } public IQueryable<Person> All. Including( params Expression<Func<Person, object>>[] include. Properties) { } public void Delete(int id){ var person = context. People. Find(id); context. People. Remove(person); } IQueryable<Person> query = context. People; foreach (var include. Property in include. Properties) { query = query. Include(include. Property); } return query; public Person Find(int id){ return context. People. Find(id); } public void Save() { context. Save. Changes(); } } public void Dispose() { context. Dispose(); }
using Contacts. Library; namespace Repo. Console. App { class Program { static void Main(string[] args) { using (var repo = new Person. Repository()) { repo. Insert. Or. Update(new Person { First. Name = "Juku", Last. Name = "Mänd" }); repo. Insert. Or. Update(new Person { First. Name = "Malle", Last. Name = "Tamm" }); repo. Save(); foreach (var person in repo. All. To. List()) { Console. Write. Line("{0} {1}", person. First. Name, person. Last. Name); } } } Console. Read. Line();
public interface IEntity. Repository<T> : IDisposable { IQueryable<T> All { get; } IQueryable<T> All. Including(params Expression<Func<T, object>>[] include. Properties); T Find(int id); void Insert. Or. Update(T entity); void Delete(int id); void Save(); } public interface IPerson. Repository : IEntity. Repository<Person> { }
// this is the base repository interface for all EF repositories public interface IEFRepository<T> : IDisposable where T : class { // get all records in table IQueryable<T> All { get; } // get all records with filter IQueryable<T> Get. All. Including (params Expression<Func<T, object>>[] include. Properties); } T Get. By. Id(int id); void Add(T entity); void Update(T entity); void Delete(int id); void Save();
// this is universal base EF repository implementation, to be included in all other repos // covers all basic crud methods, common for all other repos public class EFRepository<T> : IEFRepository<T> where T : class { // the context and the dbset we are working with protected Db. Context { get; set; } protected Db. Set<T> Db. Set { get; set; } . . . //Constructor, requires db. Context as dependency public EFRepository(Db. Context db. Context) { if (db. Context == null) throw new Argument. Null. Exception("db. Context"); Db. Context = db. Context; //get the dbset from context Db. Set = Db. Context. Set<T>(); } public IQueryable<T> All { get { return Db. Set; } }
interface IPerson. Repository : IEFRepository<Person> { } public class Person. Repository : EFRepository<Person>, IPerson. Repository { public Person. Repository(Db. Context context) : base(context) { } }













