Repository pattern Andres Käver, IT Kolledž 2016/2017 Spring.

Slides:



Advertisements
Similar presentations
Lecture 9 Concepts of Programming Languages
Advertisements

ORM Technologies and Entity Framework (EF)
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Database Design for DNN Developers Sebastian Leupold.
Connecting a.NET application to a database Jim Warren, COMPSCI 280 S Enterprise Software Development.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Testing Web Services Unit Testing, Data Layer Testing, Web API Controllers Testing, Integration Testing Web Services & Cloud SoftUni Team testing Technical.
Data File Access API : Under the Hood Simon Horwith CTO Etrilogy Ltd.
DEPENDENCY INJECTION & INVERSION OF CONTROL. WHAT’S GOING TO BE COVERED Quick intro to C# for Java developers Dependency Injection Inversion of Control.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
ILM Proprietary and Confidential -
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Domain and Persistence Patterns. Fundamental Pattern Types Design Patterns Business Logic Patterns.
ORM Technologies and Entity Framework (EF) ORM Concepts, Entity Framework, DbContext, CRUD Operations SoftUni Team Technical Trainers Software University.
Entity Framework: Code First SoftUni Team Technical Trainers Software University
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Copyright © 2007 InSTech Joint Laboratory All rights reserved. 1 Consideration on Persistence of WiseMan FuDan-Hitachi InSTech (Innovative Software Technology)
PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
Unit testing of the Services Telerik Software Academy Web Services and Cloud.
Repository muster – vol2 Mait Poska & Andres Käver, IT Kolledž 2015.
Andres Käver, IT Kolledž public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression.
COMP 430 Intro. to Database Systems SQL from application code.
Building Web Applications with Microsoft ASP
Introduction to Entity framework
Building Web Applications with Microsoft ASP
Introduction to Entity Framework
What is Laravel ? By Georgi Genov.
ASP.NET Unit Testing Unit Testing Web API SoftUni Team ASP.NET
Structure of a web application
EF Advanced Querying Optimize Performance SoftUni Team Advanced
“Packages in Java”.
Entity Framework DB From Code, OOP Introduction
C#: ASP.NET MVC Overview
Best practices and architecture
CS520 Web Programming Spring – Inversion of Control
Dependency Injection Andres Käver, IT College 2016/2017 Spring.
Content Providers.
Entity Framework By: Casey Griffin.
Factory pattern Unit of Work
Packages, Interfaces & Exception Handling
SharePoint Cloud hosted Apps
…and web frameworks in general
Lecture 9 Concepts of Programming Languages
Lecture 13 Writing Classes Richard Gesick.
Lecture 3: The J# Language
Data Structures and Database Applications Custom Models with MVC
Structured Query Language
Social Media And Global Computing Managing MVC with Custom Models
Developing a Model-View-Controller Component for Joomla Part 3
Assignment 7 User Defined Classes Part 2
IT College 2016, Andres käver
C# - EF Core IT College, Andres Käver, , Fall semester
…and web frameworks in general
Implementing Entity Framework with MVC Jump Start
C# - EF Core Intro IT College, Andres Käver, , Fall semester
Designing For Testability
Existing SQL Integration
C# - Razor Pages Db/Scaffolding/Async
Web APIs In computer programming, an application programming interface (API) is a set of subroutine definitions, protocols, and tools for building application.
06 | Integrating extra features and looking forward
Entity Framework & LINQ (Language Integrated Query)
Dependency Injection Mechanism
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Repository pattern Andres Käver, IT Kolledž 2016/2017 Spring

Repository Capsulate data processing Classical way: SqlCommand(”SELECT * FROM Customer where ID=“ + cid).Execute Create class instance Copy result data into object Repo: GetCustomerById(cid) Repo user is not aware where and how is data stored and retrieved (sql, web-api, xml, csv files,....)

Repository How many and what types of repos to create? One per class/table Graph based Write only repo, read only repo One huge repo No right answers, you have to decide Every project and team has its own customs and requirements

Repository – template – not recommended Install-package t4scaffolding.VS2015 Scaffold repository ClassName ContextClass

Repository - interface public interface IPersonRepository : IDisposable { IQueryable<Person> All { get; } IQueryable<Person> AllIncluding( params Expression<Func<Person, object>>[] includeProperties); Person Find(int id); void InsertOrUpdate(Person person); void Delete(int id); void Save(); } Next developer should only look at the repo interface. How repo operates - not important. Interface is also needed for dependency injection

Repository - code 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(); public class PersonRepository : IPersonRepository { ContactContext context = new ContactContext(); public IQueryable<Person> All { get { return context.People; } } public IQueryable<Person> AllIncluding( params Expression<Func<Person, object>>[] includeProperties) { IQueryable<Person> query = context.People; foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); return query; public Person Find(int id){ return context.People.Find(id);

Using repo in console app 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();

Repository – universal 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(); } public interface IPersonRepository : IEntityRepository<Person>

Repository - Problems Object graphs Disposed context If possible, avoid graphs. Operate with single objects. (Common in web-api/mvc)

Repository – one more time Repo – container, data storage engine capsulation Typically CRUD methods, to operate on some concrete class Real data storage engine, with its implementation details is hidden from user Possibility to easily replace storage mechanism within application

Repository – generic interface // 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(); }

Repository - generic // 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; } ......

Repository – generic - incorporating interface IPersonRepository : IEFRepository<Person> { } public class PersonRepository : EFRepository<Person>, IPersonRepository public PersonRepository(DbContext context) : base(context)