Presentation is loading. Please wait.

Presentation is loading. Please wait.

Andres Käver, IT Kolledž 2016. public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression.

Similar presentations


Presentation on theme: "Andres Käver, IT Kolledž 2016. public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression."— Presentation transcript:

1 Andres Käver, IT Kolledž 2016

2

3

4

5 public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression >[] includeProperties); Person Find(int id); void InsertOrUpdate(Person person); void Delete(int id); void Save(); }

6 public class PersonRepository : IPersonRepository { ContactContext context = new ContactContext(); public IQueryable All { get { return context.People; } } public IQueryable AllIncluding( params Expression >[] includeProperties) { IQueryable query = context.People; foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } return query; } public Person Find(int id){ return context.People.Find(id); } public void InsertOrUpdate(Person person) { if (person.PersonID == default(int)) { // New entity context.People.Add(person); } else { // Existing entity context.Entry(person).State = EntityState.Modified; } public void Delete(int id){ var person = context.People.Find(id); context.People.Remove(person); } public void Save() { context.SaveChanges(); } public void Dispose() { context.Dispose(); }

7 using ContactsLibrary; namespace RepoConsoleApp { class Program { static void Main(string[] args) { using (var repo = new PersonRepository()) { repo.InsertOrUpdate(new Person { FirstName = "Juku", LastName = "Mänd" }); repo.InsertOrUpdate(new Person { FirstName = "Malle", LastName = "Tamm" }); repo.Save(); foreach (var person in repo.All.ToList()) { Console.WriteLine("{0} {1}", person.FirstName, person.LastName); } Console.ReadLine(); }

8 public interface IEntityRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding(params Expression >[] includeProperties); T Find(int id); void InsertOrUpdate(T entity); void Delete(int id); void Save(); } public interface IPersonRepository : IEntityRepository { }

9

10

11 // this is the base repository interface for all EF repositories public interface IEFRepository : IDisposable where T : class { // get all records in table IQueryable All { get; } // get all records with filter IQueryable GetAllIncluding (params Expression >[] includeProperties); T GetById(int id); void Add(T entity); void Update(T entity); void Delete(T entity); void Delete(int id); void Save(); }

12 // 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 : IEFRepository where T : class { // the context and the dbset we are working with protected DbContext DbContext { get; set; } protected DbSet DbSet { get; set; } //Constructor, requires dbContext as dependency public EFRepository(DbContext dbContext) { if (dbContext == null) throw new ArgumentNullException("dbContext"); DbContext = dbContext; //get the dbset from context DbSet = DbContext.Set (); } public IQueryable All { get { return DbSet; } }......

13 interface IPersonRepository : IEFRepository { } public class PersonRepository : EFRepository, IPersonRepository { public PersonRepository(DbContext context) : base(context) { }


Download ppt "Andres Käver, IT Kolledž 2016. public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression."

Similar presentations


Ads by Google