Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repository muster – vol2 Mait Poska & Andres Käver, IT Kolledž 2015.

Similar presentations


Presentation on theme: "Repository muster – vol2 Mait Poska & Andres Käver, IT Kolledž 2015."— Presentation transcript:

1 Repository muster – vol2 Mait Poska & Andres Käver, IT Kolledž 2015

2 Repositoorium väga 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

3 Repo interface 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(); } 3

4 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

5 Veelgi universaalsem repo interface - generic // 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(); } 5

6 Inkorporeerimine universaalse repo liidese spetsiifilisega 6 public interface IPersonRepository : IEFRepository { }

7 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 : 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; } }...... 7

8 Generic repo – liitmine spetsiifilisse reposse interface IPersonRepository : IEFRepository { } public class PersonRepository : EFRepository, IPersonRepository { public PersonRepository(DbContext context) : base(context) { } 8

9 Demo 9


Download ppt "Repository muster – vol2 Mait Poska & Andres Käver, IT Kolledž 2015."

Similar presentations


Ads by Google