generic repository pattern c#
C#
// IGenericRepository.cs :
namespace RepositoryUsingEFinMVC.GenericRepository
{
public interface IGenericRepository<T> where T : class
{
IEnumerable<T> GetAll();
T GetById(object id);
void Insert(T obj);
void Update(T obj);
void Delete(object id);
void Save();
}
}
// GenericRepository.cs :
namespace RepositoryUsingEFinMVC.GenericRepository
{
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private EmployeeDBContext _context = null;
private DbSet<T> table = null;
public GenericRepository()
{
this._context = new EmployeeDBContext();
table = _context.Set<T>();
}
public GenericRepository(EmployeeDBContext _context)
{
this._context = _context;
table = _context.Set<T>();
}
public IEnumerable<T> GetAll()
{
return table.ToList();
}
public T GetById(object id)
{
return table.Find(id);
}
public void Insert(T obj)
{
table.Add(obj);
}
public void Update(T obj)
{
table.Attach(obj);
_context.Entry(obj).State = EntityState.Modified;
}
public void Delete(object id)
{
T existing = table.Find(id);
table.Remove(existing);
}
public void Save()
{
_context.SaveChanges();
}
}
}
// Main:
using System.Web.Mvc;
using RepositoryUsingEFinMVC.DAL;
using RepositoryUsingEFinMVC.GenericRepository;
namespace RepositoryUsingEFinMVC.Controllers
{
public class EmployeeController : Controller
{
private IGenericRepository<Employee> repository = null;
public EmployeeController()
{
this.repository = new GenericRepository<Employee>();
}
public EmployeeController(IGenericRepository<Employee> repository)
{
this.repository = repository;
}
[HttpGet]
public ActionResult Index()
{
var model = repository.GetAll();
return View(model);
}
[HttpGet]
public ActionResult AddEmployee()
{
return View();
}
[HttpPost]
public ActionResult AddEmployee(Employee model)
{
if (ModelState.IsValid)
{
repository.Insert(model);
repository.Save();
return RedirectToAction("Index", "Employee");
}
return View();
}
[HttpGet]
public ActionResult EditEmployee(int EmployeeId)
{
Employee model = repository.GetById(EmployeeId);
return View(model);
}
[HttpPost]
public ActionResult EditEmployee(Employee model)
{
if (ModelState.IsValid)
{
repository.Update(model);
repository.Save();
return RedirectToAction("Index", "Employee");
}
else
{
return View(model);
}
}
[HttpGet]
public ActionResult DeleteEmployee(int EmployeeId)
{
Employee model = repository.GetById(EmployeeId);
return View(model);
}
[HttpPost]
public ActionResult Delete(int EmployeeID)
{
repository.Delete(EmployeeID);
repository.Save();
return RedirectToAction("Index", "Employee");
}
}
}
Also in C#:
- Title
- convert string to datetime c#
- Category
- C#
- Title
- c# webcam
- Category
- C#
- Title
- c# 2 timespan return yesterday
- Category
- C#
- Title
- c# using get set methods
- Category
- C#
- Title
- iformfile blobclient
- Category
- C#
- Title
- xarray: create 2d dataset
- Category
- C#
- Title
- C# check if is first run
- Category
- C#
- Title
- unity oncollisionenter2d
- Category
- C#
- Title
- unity pro version free download
- Category
- C#
- Title
- CS0103 C# The name 'Request.Url.Scheme' does not exist in the current context
- Category
- C#
- Title
- when do i need to end a sentence with ; in c#
- Category
- C#
- Title
- get first and last item list c#
- Category
- C#
- Title
- unity how to get transform scale
- Category
- C#
- Title
- c# list.foreach
- Category
- C#
- Title
- csharp datetime string format
- Category
- C#
- Title
- list clone - C#
- Category
- C#
- Title
- how to do a messagebox in c#
- Category
- C#
- Title
- find gameobject with tag
- Category
- C#
- Title
- Move player on planets in unity 2d
- Category
- C#
- Title
- button commandfield commandargument pass textbox
- Category
- C#
- Title
- c# making a folder
- Category
- C#
- Title
- set int to null c#
- Category
- C#
- Title
- get user directory of file in c#
- Category
- C#
- Title
- convert array object to int[] c#
- Category
- C#
- Title
- how to track a branch on github
- Category
- C#
- Title
- unity invoke
- Category
- C#
- Title
- c# combobox datasource enum
- Category
- C#
- Title
- c# inline initialize dictionary
- Category
- C#
- Title
- how to change scenes in unity
- Category
- C#
- Title
- C# get pc language
- Category
- C#
- Title
- move towards target unity
- Category
- C#
- Title
- office open xml check if row is empty
- Category
- C#
- Title
- read configuration workerservice
- Category
- C#
- Title
- unity how to set an objects postion x,y,z
- Category
- C#
- Title
- unity quaternion
- Category
- C#
- Title
- C# save pdf stream to file
- Category
- C#
- Title
- how to create a list in c# unity
- Category
- C#
- Title
- array copy c#
- Category
- C#
- Title
- how to make an array in csharp
- Category
- C#
- Title
- public enum c#
- Category
- C#
- Title
- convert string array to int C#
- Category
- C#
- Title
- how to open new form on button click in c# xamarin
- Category
- C#
- Title
- c# uppercase string
- Category
- C#
- Title
- how to create and trigger a function unity animation events
- Category
- C#
- Title
- unity get component
- Category
- C#
- Title
- unity list
- Category
- C#
- Title
- mvc write to console
- Category
- C#
- Title
- c# split a string and return list
- Category
- C#
- Title
- snippet to create constructor in asp.net c#
- Category
- C#
- Title
- C# .net core convert string to enum
- Category
- C#
- Title
- unity main texture not working
- Category
- C#
- Title
- chase object unity
- Category
- C#
- Title
- vb.net drag window without titlebar
- Category
- C#
- Title
- unity load text resources from subfolder
- Category
- C#
- Title
- c# double value with 2 decimal places
- Category
- C#
- Title
- how to copy one array value to another without reference c#
- Category
- C#
- Title
- replace all ponctuation characters c#
- Category
- C#
- Title
- c# how to sort a list
- Category
- C#
- Title
- c# winform remove button border
- Category
- C#
- Title
- c# get time
- Category
- C#
- Title
- array syntax c#
- Category
- C#
- Title
- find month number from date C#
- Category
- C#
- Title
- c# read all text from a file
- Category
- C#
- Title
- move character unity
- Category
- C#
- Title
- .net core identity get user id
- Category
- C#
- Title
- landscape print gridcontrol devexpress
- Category
- C#
- Title
- check if number is even or odd c#
- Category
- C#
- Title
- unity on trigger enter
- Category
- C#
- Title
- how consider the first caracter in Split c#
- Category
- C#
- Title
- c# console delay
- Category
- C#
- Title
- unity hide mouse
- Category
- C#
- Title
- c# loop through two dimensional array
- Category
- C#
- Title
- sum of digits in c#
- Category
- C#
- Title
- linq c# where condition
- Category
- C#
- Title
- unity cast float to int
- Category
- C#
- Title
- unity gameobjects with tag
- Category
- C#
- Title
- degree between two points latitude longitude c#
- Category
- C#
- Title
- C# Unknown column 'FundAllocation' in 'field list
- Category
- C#
- Title
- how to make % posibility to spawn an object C# in unity
- Category
- C#
- Title
- never lose focus textbox c#
- Category
- C#
- Title
- run async methods within a sync process
- Category
- C#
- Title
- C# foreach loop async but wait at end
- Category
- C#
- Title
- unity hide mouse first person
- Category
- C#
- Title
- compile in one single exe c#
- Category
- C#
- Title
- make camera follow character unity 2020
- Category
- C#
- Title
- linq query select top 1 c#
- Category
- C#
- Title
- convert string to boolean c#
- Category
- C#
- Title
- countdownevent
- Category
- C#
- Title
- c# how to use inovke
- Category
- C#
- Title
- unity rotation
- Category
- C#
- Title
- enums as numbers c#
- Category
- C#
- Title
- C# enum
- Category
- C#
- Title
- c# System.Resources.MissingManifestResourceException error
- Category
- C#
- Title
- c sharp substring
- Category
- C#
- Title
- appsettings in console application c#
- Category
- C#
- Title
- wpf use enum description
- Category
- C#
- Title
- unity making homing missile
- Category
- C#
- Title
- bold caption latex
- Category
- C#
- Title
- how to clear console in c#
- Category
- C#
- Title
- c# string remove special characters
- Category
- C#