MVC - Repository-And-Unit-Of-Work

Similar documents
Repository Pattern and Unit of Work with Entity Framework

Melon, Inc. Melon Components Starter Kit

MVC 4 Setup and Configuration Training Document. Prepared by Andre Masters. Rev 1.0

Domain Driven Design, MVC & Entity Framework

MVC :: Understanding Models, Views, and Controllers

Using Visual Studio 2017

Developing Mobile Apps with Xamarin and Azure

Babu Madhav Institute of information technology 2016

LAMPIRAN. 1. Source Code Data Buku. using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.

Azure Developer Immersion Redis Cache

MVC CRUD. Tables: 1) Dinners (First Table)

SQLite Database. References. Overview. Structured Databases

E-Commerce Web Application

MVC :: Preventing JavaScript Injection Attacks. What is a JavaScript Injection Attack?

Working with Controllers

emkt Browserless Coding For C#.Net and Excel

.NET Database Technologies. Entity Framework: Queries and Transactions

Item 18: Implement the Standard Dispose Pattern

This tutorial is designed for software programmers who would like to learn the basics of ASP.NET Core from scratch.

5 Years Integrated M.Sc. (IT) 6th Semester Web Development using ASP.NET MVC Practical List 2016

When you create a new ASP.NET MVC application, the application is already configured to use URL Routing. URL Routing is setup in two places.

Supporting Non-Standard Development Configurations

Activating AspxCodeGen 4.0

Bringing Together One ASP.NET

Don Smith, Program Manager Microsoft patterns & practices

Quick Start - WPF. Chapter 4. Table of Contents

QDABRA DBXL S XML RENDERING SERVICE CONFIGURATION

.NET Web Applications. Example Project Walk-Through

Use the TeamCity plugin

Chapter 1 Getting Started

MVC :: Understanding Views, View Data, and HTML Helpers

EVALUATION COPY. Unauthorized reproduction or distribution is prohibitied ASP.NET MVC USING C#

Representing Recursive Relationships Using REP++ TreeView

EXERCISE 3: PUBLISHING AN ODM DATABASE WITH WATERONEFLOW

This walkthrough assumes you have completed the Getting Started walkthrough and the first lift and shift walkthrough.

Objectives. Introduce static keyword examine syntax describe common uses

Workspace Administrator Help File

Software Systems Development Unit AS1: Introduction to Object Oriented Development

The contents of this document are directly taken from the EPiServer SDK. Please see the SDK for further technical information about EPiServer.

A-1. Installing SQL Server 2016 Developer. Downloading Microsoft SQL Server 2016 Developer

Index. Alessandro Del Sole 2017 A. Del Sole, Beginning Visual Studio for Mac,

HangFire Documentation

TARGETPROCESS PLUGIN DEVELOPMENT GUIDE

Getting Started with the Bullhorn SOAP API and C#/.NET

Building RESTful Web Services with. Presented by Steve Ives

Index. Bower, 133, 352 bower.json file, 376 Bundling files, 157

NerdDinner Step 1: File->New Project

Full Stack.Net Developer Course

Using the PDSA Security System

Developing and deploying MVC4/HTML5 SDK application to IIS DigitalOfficePro Inc.

C:\Temp\Templates. Download This PDF From The Web Site

Web Sites in Production

Creating the Data Layer

Hands-On Lab. Getting Started with Git using Team Foundation Server Lab version: Last updated: 12/30/2013

Haystack Overview. Chapter 1. Table of Contents

ADO.NET 2.0. database programming with

Colectica Workflow Deployment

10267 Introduction to Web Development with Microsoft Visual Studio 2010

Instructions for Using PEP Enhancements. Create a Template for Multiple Enrollment Applications

Getting Started Using HBase in Microsoft Azure HDInsight

T, Earl Grey, Hot: Generics in.net

Exam Questions

Using the SQL CI TeamCity plugin in SQL Automation Pack

Chapter 12: How to Create and Use Classes

Entity Framework. #entityframework

DEVELOPING WEB AZURE AND WEB SERVICES MICROSOFT WINDOWS AZURE

ASP.NET MVC 3 Using C# Rev. 3.0

Justin Jones 21 st February 2013 Version 1.1 Setting up an MVC4 Multi-Tenant Site

ASP.NET MVC 5. Nemanja Kojic, MScEE

The connection has timed out

Introduction to Prime 4. Prerequisites 5. Deploying the Database 7. Updating the Web.Config 10. Turn-Off SharePoint Mobile 13

Linked List using a Sentinel

@ Jon Hilton All rights reserved.

PISCES Installation and Getting Started 1

Volume CREATIVE DATA TECHNOLOGIES, INC. DATALAYER.NET. Getting Started Guide

.Net Job Assured Course (3 in 1)

Grading Rubric Homework 1

Drop Windows Login Sql Server 2005 Connection String In Web.config

Engr 123 Spring 2018 Notes on Visual Studio

Microsoft Visual C# Step by Step. John Sharp

Forrest Terrace, Abbotsford, BC V2S 1G7 Tel: Fax: Website:

Click Studios. Passwordstate. Upgrade Instructions to V8 from V5.xx

WF-distiller Installation Guide

Copy Datatable Schema To Another Datatable Vb.net

The following instructions cover how to edit an existing report in IBM Cognos Analytics.

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

CPET 499/ITC 250 Web Systems. Topics

Chapter 1: Object-Oriented Programming Using C++

Integrate WebScheduler to Microsoft SharePoint 2007

Request for Quote (RFQ)

Overview... 7 Tabs... 8 Selected Tables... 8 Load Tables Button Selected Views... 8 Load Views Button Clear Selection Button...

Configuring RentalPoint Web Services

RMS Monitoring Software System Installation

Asp Net Mvc Framework Unleashed

Installation Guide of FingaPass Gateway. Doc version v1.0

Studio Productivity Tools Q2 2013

How to create an Add-In extension.dll file and make it available from Robot pull down menu. (language C#)

MSSQL and.net. Using SQL in.net, running.net code in SQL Server

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

TABLE OF CONTENTS. Data binding Datagrid 10 ComboBox 10 DropdownList 10. Special functions LoadFromSql* 11 FromXml/ToXml 12

Transcription:

MVC - Repository-And-Unit-Of-Work What are the Repository and Unit of Work Design Patterns? When you set up an ASP.NET MVC project with the Entity framework, you can think of your project as consisting of four major parts: the first three are the obvious Models, Views and Controllers that make up the MVC project and the fourth is the data access layer where you make use of the Entity framework to retrieve data from a data source. The question is, how should we connect our data access layer to the parts of our MVC project? The quickest way is to simply create an instance of your database context in your controller, giving the controller direct access to the database context as illustrated here: Normal MVC Project A repository is a class that encapsulates the CRUD operations for an entity. As such, the Repository pattern allows us to write the data access logic for an entity in one place. This is good because if the data access logic needs to change, we don't even need to touch the controllers. This makes your project much more maintainable. What we could do now is give the controllers direct access to the repositories that they need as illustrated below.

MVC With Repository: But giving your controllers direct access to repositories like this can still lead to problems. Firstly, where should the database context go? After all, each repository needs access to a database context to perform its data access operations and the database context's SaveChanges() method must be called from somewhere to save any changes to the database. How should we go about doing this? One option is that we can create an instance of the database context in the controllers and pass it to the repositories. This is where the Unit of Work design pattern comes to the rescue. It functions as a centralized place to house your repositories using a single instance of a database context to access and save data. Giving the controllers access to a UnitOfWork class as a means to retrieve and update data solves many of the above mentioned issues

Fig:MVC Repository & Unit Of Work In this figure, we see the controller only gets direct access to the UnitOfWork class. The UnitOfWork class handles the access of and updates to all the repositories using the database context. Since the UnitOfWork class encapsulates the repositories and the database context, we have the freedom to easily change the implementation of our data access layer without having to change the controller's logic. Follow below steps to create MVC Project: Open the Visual Studio and click File New Project. From the left pane, select Templates Visual C# Web.(ASP.NET Web Application )

By Clicking Ok it prompts window Now Add Controller: ControllersAddController

By Clicking opens window: Now Create View for Controller: Right Click on IndexAdd View

Opens window: Select Template : Empty So finally it looks like: We are finished with the MVC portion of the project so we will move on to the entities, that is, the classes containing the data you want to display and/or modify on your web page Create a new Class Library project in the solution. These will be classes that model domain objects as well as the classes that interact with the data source. Create the Class Library in your solution:

SolutionAddNewProject From the left pane, select Templates Visual C# ClassLibrary Add 3 Folders in ClassLibrary Project name as Abstract,Concrete and Entities Abstract folder is for interfaces and/or abstract classes. The Concrete folder will hold implementations of these classes as well as other data access classes. The Entities folder will hold POCO domain object classes. POCO. A simple object without complicated logic which doesn't require to be identifiable, usually it has just a few properties and is used with ORM or as a Data Transfer Object

Finally Stucture looks like : Since we want to be accessing classes from the class library you will need to add a reference to it in your MVC project. To do so, MVC project References Right Click Add Reference and add a reference to Class Library Project.

Now Open the Nuget Package Manager and install the Entity Framework. Create an Album and Account class in the Entities folder like this: namespace RepoAndUnitOfWork.Domain.Entities public class Album public int AlbumId get; set; public string Title get; set; public string Artist get; set;

namespace RepoAndUnitOfWork.Domain.Entities public class Account public int AccountId get; set; public string UserName get; set; public string Password get; set; Now we can establish our database connection. I will just use the SQL Server LocalDB. Firstly, add the database connection by going to Tools > 'Connect to Database...' and filling out the form like this:

Next, in the RepoAndUnitOfWork MVC project add the following connection string to the web.config file : <connectionstrings> <add name="musicstoredbcontext" connectionstring="data Source=(localdb)\v11.0;Initial Catalog=MusicStoreWebsite;Integrated Security=True" providername="system.data.sqlclient" /> </connectionstrings>

Now, in the Concrete folder, add the MusicStoreDbContext class: MusicStoreDbContext.cs using System.Data.Entity; using RepoAndUnitOfWork.Domain.Entities; namespace RepoAndUnitOfWork.Domain.Concrete public class MusicStoreDbContext : DbContext public DbSet<Album> Albums get; set; public DbSet<Account> Accounts get; set; Add some sample data to the database. To do so, we will just use our HomeController in the Controllers folder of the MVC project. HomeController: using System.Web.Mvc; using RepoAndUnitOfWork.Domain.Concrete; using RepoAndUnitOfWork.Domain.Entities; namespace RepoAndUnitOfWork.Controllers public class HomeController : Controller MusicStoreDbContext dbcontext = new MusicStoreDbContext(); public ActionResult Index() dbcontext.albums.add(new AlbumAlbumId = 1, Title = "Odelay", Artist = "Beck"); dbcontext.accounts.add(new Account AccountId = 1, UserName = "JaneDoe", Password = "123456"); dbcontext.accounts.add(new Account AccountId = 2, UserName = "user18081971", Password = "QKThr" );

dbcontext.savechanges(); return View(); Now, build and run the solution This will call the HomeController's Index() method and create the tables in your database and populate them with data. To verify the date is added, you can check out your database in the server explorer which should now have the tables with the added data.: Now Set Up is ready, so we will implement Repository and Unit of Work design patterns. Creating the Repositories To begin, create the following repository interface in the Abstract folder of your ClassLibray project. IRepository.cs using System; using System.Collections.Generic; namespace RepoAndUnitOfWork.Domain.Abstract public interface IRepository<T> : IDisposable where T : class //Method to get all rows in a table IEnumerable<T> DataSet get; //Method to add row to the table void Create(T entity); //Method to fetch row from the table T GetById(int? id);

//Method to update a row in the table void Update(T entity); //Method to delete a row from the table void Delete(T entity); This generic interface merely serves as a contract for all repositories They all must have implementations of these methods Next, in the Concrete folder, create the following generic class which implements IRepository Repository.cs using System; using System.Collections.Generic; using System.Data.Entity; using RepoAndUnitOfWork.Domain.Abstract; namespace RepoAndUnitOfWork.Domain.Concrete public class Repository<T> : IRepository<T>, IDisposable where T : class protected MusicStoreDbContext dbcontext; //DbSet usable with any of our entity types protected DbSet<T> dbset; //constructor taking the database context and getting the appropriately typed data set from it public Repository(MusicStoreDbContext context) dbcontext = context; dbset = context.set<t>();

//Implementation of IRepository methods public virtual IEnumerable<T> DataSet get return dbset; public virtual void Create(T entity) dbset.add(entity); public virtual T GetById(int? id) return dbset.find(id); public virtual void Update(T entity) dbcontext.entry(entity).state = EntityState.Modified; public virtual void Delete(T entity) dbset.remove(entity); //IDisposable implementation private bool disposed = false; protected virtual void Dispose(bool disposing)

if (!this.disposed) if (disposing) dbcontext.dispose(); public void Dispose() Dispose(true); GC.SuppressFinalize(this); As you can see, this class includes implementations of IRepository's create, read, update and delete operations. It is a generic class because we can reuse it to deal with different types of entities. Its usage will become clearer when we create the UnitOfWork class. At this stage, the repository portion of the Repository and Unit of Work Patterns is complete. Creating the Unit of Work Class Now that we have our repository class set up, we can now create our unit of work class. This class will be used to provide access to and save changes made to our repositories, all using a single database context. UnifOfWork.cs using System; using RepoAndUnitOfWork.Domain.Entities; using RepoAndUnitOfWork.Domain.Abstract; namespace RepoAndUnitOfWork.Domain.Concrete public class UnitOfWork : IDisposable

//Our database context private MusicStoreDbContext dbcontext = new MusicStoreDbContext(); //Private members corresponding to each concrete repository private Repository<Account> accountrepository; private Repository<Album> albumrepository; null //Accessors for each private repository, creates repository if public IRepository<Account> AccountRepository get if (accountrepository == null) accountrepository = new Repository<Account>(dbContext); return accountrepository; public IRepository<Album> AlbumRepository get if (albumrepository == null) albumrepository = new Repository<Album>(dbContext);

return albumrepository; //Method to save all changes to repositories public void Commit() dbcontext.savechanges(); //IDisposible implementation private bool disposed = false; protected virtual void Dispose(bool disposing) if (!this.disposed) if (disposing) dbcontext.dispose(); public void Dispose() Dispose(true); GC.SuppressFinalize(this);

How to Use the Unit of Work Class With our UnitOfWork class complete we can now use it to access and modify data in our MVC project. To do so, simply create an instance of UnitOfWork in your controller like so. HomeController.cs using System.Web.Mvc; using RepoAndUnitOfWork.Domain.Concrete; namespace RepoAndUnitOfWork.Controllers public class HomeController : Controller UnitOfWork unitofwork = new UnitOfWork(); public ActionResult Index() return View(); Now you can access and modify data within your controller In the Models folder of the MVC project, create the following view model called HomeModel HomeModel.cs using RepoAndUnitOfWork.Domain.Concrete; using RepoAndUnitOfWork.Domain.Entities; namespace RepoAndUnitOfWork.Models public class HomeModel public Album Album get; set;

public Account Account get; set; public HomeModel(UnitOfWork unitofwork, int? albumid, int? accountid) Album = unitofwork.albumrepository.getbyid(albumid); Account = unitofwork.accountrepository.getbyid(accountid); Now, in your controller, all you need to do is create an instance of HomeModel and pass it to the view. Then, all data within the view model (all albums and accounts) will be accessible to your view HomeController.cs using System.Web.Mvc; using RepoAndUnitOfWork.Domain.Concrete; using RepoAndUnitOfWork.Models; namespace RepoAndUnitOfWork.Controllers public class HomeController : Controller UnitOfWork unitofwork = new UnitOfWork(); public ActionResult Index() HomeModel model = new HomeModel(unitOfWork, 1, 2); return View(model); So finally Our Stucture looks like :

Now Run the Project Your o/p: