Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
One of the most common pattern is followed in the world of Entity Framework is “Repository Pattern”. Since this is something which is heavily used and being practiced, I am not going to talk about the core pattern. Rather, try to show how one can implement it.
Objectives
As mentioned in https://msdn.microsoft.com/en-us/library/ff649690.aspx
Simple approach to ADO.NET Entity Framework
Let’s have one domain class called “Employee”
public class Employee
{
public int Id { get; set; }
public string FullName { get; set; }
}
Now using this we will have a simple context class
public class HRContext : DbContext
{
public DbSet<DomainClasses.Employee> Employees { get; set; }
}
After that, define the repository interface IEmployeeRepository
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();
}
Then the Repository class called EmployeeRepository
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();
}
}
Then you should be implementing it in your apps (any type Windows or Web), like a Console Application
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
GetSomeEmployee();
}
private static void IntiateData()
{
using (var repo = new EmployeeRepository())
{
Employee em = new Employee() { FullName = "Wriju" };
repo.InsertOrUpdate(em);
repo.Save();
}
}
private static void GetSomeEmployee()
{
using (var repo = new EmployeeRepository())
{
foreach (var emp in repo.All)
{
Console.WriteLine("{0} - {1}", emp.Id, emp.FullName);
}
}
}
}
}
This obviously simple approach. The recommended options are to make the Repository generic and handle the related entities. I will discuss about them later.
Namoskar!!!
IQueryable
, it means, its end is open. the consumer is able to do whatever he wants to that expression
and changing the propose of the method completely.Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in