Repository Pattern with ADO.NET Entity Framework Hack
Let’s build ADO.NET Entity Framework library using Repository Pattern. There are a many ways but this is probably the easiest hack.
First build a blank solution
Then add a class library called “DAL” and add a class called “Employee”
namespace DAL
{
public class Employee
{
public int Id { get; set; }
public string FullName { get; set; }
}
}
Then in the DAL add “EntityFramework” from https://nuget.org in Package Manager Console
At Package manager Console it would show
Then add T4Scaffolding from https://nuget.org in Package Manager Console
The Package Manager Console would look like
Project structure will be as below
Notice that in the reference section the Entity Framework is showing but the “T4Scaffolding” is missing. However the packages.config shows the list.
This is as template so you will find them at the source folder
Run the following command from the DAL project’s Package Manager Console
PM> scaffold repository DAL.Employee -DbContextType:HRContext
Output would show
This would create two files under DAL > Models as “EmployeeReopository.cs” and “HRContext.cs”
The content would look like
HRContext.cs
namespace DAL.Models
{
public class HRContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, add the following
// code to the Application_Start method in your Global.asax file.
// Note: this will destroy and re-create your database with every model change.
//
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<DAL.Models.HRContext>());
public DbSet<DAL.Employee> Employees { get; set; }
}
}
EmployeeRepository.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using DAL;
namespace DAL.Models
{
public class EmployeeRepository : IEmployeeRepository
{
HRContext context = new HRContext();
public IQueryable<Employee> All
{
get { return context.Employees; }
}
public IQueryable<Employee> AllIncluding(params Expression<Func<Employee, object>>[] includeProperties)
{
IQueryable<Employee> query = context.Employees;
foreach (var includeProperty in includeProperties) {
query = query.Include(includeProperty);
}
return query;
}
public Employee Find(int id)
{
return context.Employees.Find(id);
}
public void InsertOrUpdate(Employee employee)
{
if (employee.Id == default(int)) {
// New entity
context.Employees.Add(employee);
} else {
// Existing entity
context.Entry(employee).State = EntityState.Modified;
}
}
public void Delete(int id)
{
var employee = context.Employees.Find(id);
context.Employees.Remove(employee);
}
public void Save()
{
context.SaveChanges();
}
public void Dispose()
{
context.Dispose();
}
}
public interface IEmployeeRepository : IDisposable
{
IQueryable<Employee> All { get; }
IQueryable<Employee> AllIncluding(params Expression<Func<Employee, object>>[] includeProperties);
Employee Find(int id);
void InsertOrUpdate(Employee employee);
void Delete(int id);
void Save();
}
}
You are simply done. You need a project and add DAL as reference then …
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
InsertEmployee();
}
private static void InsertEmployee()
{
using (var repo = new EmployeeRepository())
{
Employee em = new Employee() { FullName = "Wriju" };
repo.InsertOrUpdate(em);
repo.Save();
}
}
}
}
No EntityFramework reference or so.
Isn’t it easy? Enjoy!!!
Special thanks to Julie Lerman, if you don’t know who she is then you are not an Entity Framework developer.
Namoskar!!!