
Advanced Repository Concepts in ASP.NET MVC Development
Explore advanced repository concepts such as encapsulation of repetitive data access code, use of interfaces for entity repositories, and implementation of generic repository interfaces for EF repositories in ASP.NET MVC development. Learn how these concepts can simplify data handling and enhance code reusability.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Repository muster vol2 ASP.NET ASP.NET MVC MVC Mait Poska & Andres K ver, IT Kolled 2013
Repositoorium vga lihtsalt Kapsuleerida korduv andmet tluskood SqlCommand( SELECT * FROM Customer where ID= + cid).Execute Loo klassi instants Kopeeri p ringu tulemused olemisse Repo: GetCustomerById(cid) Repo kasutaja ei pruugi teadagi, kus ja kuidas andmeid hoitakse (SQL, Web API, XML-fail jne) 2
Repo interface public interface IEntityRepository<T> : IDisposable { IQueryable<T> All { get; } IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties); T Find(int id); void InsertOrUpdate(T entity); void Delete(int id); void Save(); } 3
Repo ja baas Repo konteiner (hoidja) vahepuhver ja andmehoidla kapsulatsioon T piliselt CRUD-meetodid, tegelemaks mingi konkreetse olemiga Reaalne andmehoidla koos oma detailidega on repo kasutaja eest peidetud 4
Veelgi universaalsem repo interface - generic // 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> GetAllIncluding (params Expression<Func<T, object>>[] includeProperties); T GetById(int id); void Add(T entity); void Update(T entity); void Delete(T entity); void Delete(int id); void Save(); } 5
Inkorporeerimine universaalse repo liidese spetsiifilisega public interface IPersonRepository : IEFRepository<Person> { } 6
Generic repo - realisatsioon // 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 DbContext DbContext { get; set; } protected DbSet<T> 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<T>(); } public IQueryable<T> All { get { return DbSet; } } ...... 7
Generic repo liitmine spetsiifilisse reposse interface IPersonRepository : IEFRepository<Person> { } public class PersonRepository : EFRepository<Person>, IPersonRepository { public PersonRepository(DbContext context) : base(context) { } } 8
Demo 9