Generic Repository Pattern with ASP.NET MVC and EF Md. Mahedee Hasan Microsoft MVP | Trainer | Speaker Software Architect LeadSoft Bangladesh Limited Linkedin: http://www.linkedin.com/in/mahedee Blog: http://mahedee.net/ 1
• MVC Stands for Model – View – Controller • It is Software Architectural pattern MAHEDEE.NET 2 What is MVC?
• Is an open source web application framework • It implements the model-view-controller pattern • MVC design pattern aims to “Separation Of concern” MAHEDEE.NET 3 What is ASP.NET MVC?
• Entity Framework is an ORM. • Work with relational data using domain-specific objects • Eliminates the need for most of the data-access code MAHEDEE.NET 4 What is ASP.NET EF?
DRY? Don’t repeat yourself -Means don't write duplicate code Use generic repository pattern to implement DRY MAHEDEE.NET 5
• What is Design Pattern? • Design pattern is a solution of known problems • What is Software Design Pattern? • Is a solution of known problems in Software. • Strategies of solving commonly occurring problems. • A design pattern is not a finish design. • It is like a template to solve a problem. MAHEDEE.NET 6 Software Design Pattern
Repository Pattern • Mediator between BLL and DAL(Data Source) • It’s a separation layer between Data and Domain Layer – Separates data and domain Layer MAHEDEE.NET 7
Benefits of Repository Pattern • Centralizes data logic or service logic. • Provides a substitution point for the unit tests – Both BLL and DAL • Provides a flexible architecture – Can adopt new change easily • Domain driven development is easier MAHEDEE.NET 8
What is Generic Repository Pattern? • Generally - one repository for one model to access data. – Write similar code again and again • What if Single Repository for all? • Single repository for data access of all models. MAHEDEE.NET 9
Benefits of Generic Repository Pattern • Reduce redundancy of code • Faster development • Force developer to work same pattern – Possibility of less error or no error • Easy to maintain – Centralize data access logic MAHEDEE.NET 10
Implementation • Tools and Technology used – Visual Studio 2013 – Visual C# – ASP.NET MVC 5 – Entity Framework 6 – Razor view engine MAHEDEE.NET 11
Step 1: Create an ASP.NET MVC 5 application using Visual Studio 2013 MAHEDEE.NET 12 Implementation…
Step 2: Configure connection string in web.config MAHEDEE.NET 13 Implementation …
Step 3: Create Models MAHEDEE.NET 14 Implementation …
Step 4: Create a DbContext in Repository folder. MAHEDEE.NET 15 Implementation …
Step 5: Create IGenericRepository and GenericRepository in Repository folder MAHEDEE.NET 16 Implementation …
• Step 6: – Create controllers of each models – Select template “MVC5 Controller with views, using Entity Framework” – Modify Controllers and use Generic Repository in Controller MAHEDEE.NET 17 Implementation …
• Step 7: Add links to _Layout MAHEDEE.NET 18 Implementation …
• Step 8: Write following command in package manager console – PM> Enable-Migrations -ContextTypeName GenericRepoContext – PM> Add-Migration initialcreate – PM> Update-Database -Verbose -Force MAHEDEE.NET 19 Implementation …
Step 9: Run Project MAHEDEE.NET 20 Implementation …
21

Generic Repository Pattern with ASP.NET MVC and EF

  • 1.
    Generic Repository Pattern withASP.NET MVC and EF Md. Mahedee Hasan Microsoft MVP | Trainer | Speaker Software Architect LeadSoft Bangladesh Limited Linkedin: http://www.linkedin.com/in/mahedee Blog: http://mahedee.net/ 1
  • 2.
    • MVC Standsfor Model – View – Controller • It is Software Architectural pattern MAHEDEE.NET 2 What is MVC?
  • 3.
    • Is anopen source web application framework • It implements the model-view-controller pattern • MVC design pattern aims to “Separation Of concern” MAHEDEE.NET 3 What is ASP.NET MVC?
  • 4.
    • Entity Frameworkis an ORM. • Work with relational data using domain-specific objects • Eliminates the need for most of the data-access code MAHEDEE.NET 4 What is ASP.NET EF?
  • 5.
    DRY? Don’t repeat yourself -Meansdon't write duplicate code Use generic repository pattern to implement DRY MAHEDEE.NET 5
  • 6.
    • What isDesign Pattern? • Design pattern is a solution of known problems • What is Software Design Pattern? • Is a solution of known problems in Software. • Strategies of solving commonly occurring problems. • A design pattern is not a finish design. • It is like a template to solve a problem. MAHEDEE.NET 6 Software Design Pattern
  • 7.
    Repository Pattern • Mediatorbetween BLL and DAL(Data Source) • It’s a separation layer between Data and Domain Layer – Separates data and domain Layer MAHEDEE.NET 7
  • 8.
    Benefits of RepositoryPattern • Centralizes data logic or service logic. • Provides a substitution point for the unit tests – Both BLL and DAL • Provides a flexible architecture – Can adopt new change easily • Domain driven development is easier MAHEDEE.NET 8
  • 9.
    What is GenericRepository Pattern? • Generally - one repository for one model to access data. – Write similar code again and again • What if Single Repository for all? • Single repository for data access of all models. MAHEDEE.NET 9
  • 10.
    Benefits of GenericRepository Pattern • Reduce redundancy of code • Faster development • Force developer to work same pattern – Possibility of less error or no error • Easy to maintain – Centralize data access logic MAHEDEE.NET 10
  • 11.
    Implementation • Tools andTechnology used – Visual Studio 2013 – Visual C# – ASP.NET MVC 5 – Entity Framework 6 – Razor view engine MAHEDEE.NET 11
  • 12.
    Step 1: Createan ASP.NET MVC 5 application using Visual Studio 2013 MAHEDEE.NET 12 Implementation…
  • 13.
    Step 2: Configureconnection string in web.config MAHEDEE.NET 13 Implementation …
  • 14.
    Step 3: CreateModels MAHEDEE.NET 14 Implementation …
  • 15.
    Step 4: Createa DbContext in Repository folder. MAHEDEE.NET 15 Implementation …
  • 16.
    Step 5: CreateIGenericRepository and GenericRepository in Repository folder MAHEDEE.NET 16 Implementation …
  • 17.
    • Step 6: –Create controllers of each models – Select template “MVC5 Controller with views, using Entity Framework” – Modify Controllers and use Generic Repository in Controller MAHEDEE.NET 17 Implementation …
  • 18.
    • Step 7:Add links to _Layout MAHEDEE.NET 18 Implementation …
  • 19.
    • Step 8:Write following command in package manager console – PM> Enable-Migrations -ContextTypeName GenericRepoContext – PM> Add-Migration initialcreate – PM> Update-Database -Verbose -Force MAHEDEE.NET 19 Implementation …
  • 20.
    Step 9: RunProject MAHEDEE.NET 20 Implementation …
  • 21.

Editor's Notes

  • #5 DRY – Object Oriented design Principle. Ex. Dept Info in several code
  • #6 DRY – Object Oriented design Principle. Ex. Dept Info in several code
  • #7 DRY – Object Oriented design Principle. Ex. Dept Info in several code
  • #15 Create Designation and Employee Model in model folder Describe model and annotation
  • #16 DbContext: Simplified alternative to ObjectContext and is the primary object for interacting with a database using a specified model. DbSet: Simplified alternative to ObjectSet and is used to perform CRUD operations against a specific type from the model
  • #17 It's a type constraint on T , specifying that it must be a class.
  • #18 Create Designation Controller and Employee Controller. First Add designation and then Employee. Look corresponding view