How to create a simple data access layer Unit of Work pattern with Entity Framework

Here’s a step by step guide to implement Unit of Work to complete our simple data access layer using Entity Framework. This tutorial is written based on the previous work we did.

1. Create a Database class

Create Unit of Work DAL using_EF | SamuraiListing Example

2. Add these code then we are done. 😉

namespace SamuraiListing.Data
{
    public class Database
    {
        private readonly DatabaseContext _databaseContext = new DatabaseContext();

        public IRepository<Company> Companies
        {
            get { return new Repository<Company>(_databaseContext); }
        }

        public IRepository<Sector> Sectors
        {
            get { return new Repository<Sector>(_databaseContext); }
        }

        public IRepository CompanySectors
        {
            get { return new Repository<CompanySector>(_databaseContext);}
        }

        public void Save()
        {
            _databaseContext.SaveChanges();
        }
    }
}

3. Now we can access the database to perform CRUD operation easily.

As you can see Unit of Work implementation makes database access looks easy and intuitive. You can find out more about Repository Pattern and Unit of Work pattern online, what I showed here is only a simple example and there’s a lot more to explore and i didn’t cover in this tutorial series.

	// Read from Database
	var ourDatabase = new Database();
	var allCompanies = ourDatabase.Companies.GetAll();
	var allSectors = ourDatabase.Sectors.GetAll();

	// Add new record to Database
	var newCompany = new Company { Name = "BizVise.com", Telephone = "012-9042869", Email = "support@bizvise.com", GPS = "2.251189,102.277578" };
	ourDatabase.Companies.Add(newCompany);
	ourDatabase.Save();

	// Delete record from database
	var toBeDeleteCompanyRecord = 1;
	var companyToBeDeleted = ourDatabase.Companies.GetById(toBeDeleteCompanyRecord);
	if (companyToBeDeleted != null)
	{
		ourDatabase.Companies.Delete(companyToBeDeleted);
		ourDatabase.Save();
	}

	// Update record to Database
	var company = ourDatabase.Companies.GetById(2);
	if (company != null)
	{
		company.Email = "NewEmail@bizvise.com";
		ourDatabase.Companies.Update(company);
		ourDatabase.Save();
	}

Next, we will proceed with our SamuraiListing MVC web app implementation using this UoW and KendoUI discussed so far. Here’s the sample source code sample to this step. Thanks.

Important Note:

You are looking at one of the blog post I completed in a tutorial series about KendoUI and ASP MVC4, you can find out the Table of Content on - Simple KendoUI Example – SamuraiListing


Step 1 – How to create KendoUI ASP MVC 4 project.
Step 2 - How to Create a SQL Database Project for ASP MVC in Visual Studio 2012
Step 3 - How to create a simple data access layer using Repository Pattern with Entity Framework
Step 4 - How to create a simple data access layer Unit of Work pattern with Entity Framework
Step 5 - How to add Database and DataModel class to ASP MVC4 project.
Step 6 - How to add KendoUI Grid Control and perform CRUD with ASP MVC.

Advertisements

3 comments

  1. Pingback: How to create a simple data access layer using Repository Pattern with Entity Framework | BizVise - Melaka Software Development

  2. Pingback: Simple KendoUI Example – SamuraiListing | BizVise - Melaka Software Development

  3. Hi, great tutorials on this blog. Just one question, i am used to dispose (or inside an using statement) the dbcontext, why dont you need to do it?

    Like


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s