Repository Pattern
Repository Pattern
Step 2: Create a generic repository class for the entities so that it can be imagined
as if it was created for a single entity and hence reducing code duplicity
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
internal MvcAdvancedEntities context;
internal DbSet<TEntity> dbSet;
public GenericRepository(MvcAdvancedEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get()
{
Unit of Work Pattern: Unit of Work pattern is defined in a way that it is a single
transactional work for a set of operations including insert, update, delete etc. which
may encompass relational entities. Unit of Work comes along with the Repository
pattern to ensure keeping the database consistent.
Unit of Work pattern consists of Repositories and DBContext. Instead of having
the different DBContext instances for the respective repositories Unit of Work
pattern encompasses the same DBConext instance throughout the repositories.
This technique ensures that all the operations are committed successfully or none if
any one of them is failed in operations and this is how the database is carried out as
consistent as expected always.
Step 3: Create an interface so that other unit of work class can implement it and
hence ensuring multiple inheritance
public interface IUnitOfWork:IDisposable
{
IGenericRepository<Employee> EmployeeRepository{get;}
IGenericRepository<EmpRole> EmpRoleRepository{get;}
void Commit();
}
Step 4: Create a unit of work class so that operations can take place on the
entities as a single transactional work and hence ensuring database consistency
public class UnitOfWork : IUnitOfWork
{
private MvcAdvancedEntities context;
public UnitOfWork()
{
this.context = new MvcAdvancedEntities();
}
public UnitOfWork(MvcAdvancedEntities context)
{
this.context = context;
}
private IGenericRepository<Employee> employeeRepository;
private IGenericRepository<EmpRole> empRoleRepository;
public IGenericRepository<Employee> EmployeeRepository
{
get
{
if (this.employeeRepository == null)
this.employeeRepository = new GenericRepository<Employee>(context);
return employeeRepository;
}
}
public IGenericRepository<EmpRole> EmpRoleRepository
{
get
{
if (this.empRoleRepository == null)
this.empRoleRepository = new GenericRepository<EmpRole>(context);
return empRoleRepository;
}
}
public void Commit()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
//Free any other managed objects here.
context.Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
Controller: The ASP.NET MVC controller class is responsible for handling user input
and giving responses back to the user after getting output from the unit of work
abstraction layer.
Step 5: Create a controller class so that the controller can interact with the
database context through unit of work abstraction layer
public class EmployeeController : Controller
{
private UnitOfWork.UnitOfWork unitOfWork;
public EmployeeController()
{
unitOfWork = new UnitOfWork.UnitOfWork();
}
public EmployeeController(UnitOfWork.UnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public ActionResult Index()
{
return View();
}
public IEnumerable<Employee> GetAllEmployees()
{
return (List<Employee>)unitOfWork.EmployeeRepository.Get();
}
public string Update(Employee employee)
{
if (employee != null)
{
var emp = unitOfWork.EmployeeRepository.GetByID(employee.ID);
emp.FirstName = employee.FirstName;
emp.LastName = employee.LastName;
emp.UserName = employee.UserName;
unitOfWork.EmployeeRepository.Update(emp);
unitOfWork.Commit();
return "Record has been Updated";
}
else
{
return "Record has not been Updated";
}
}
catch
{
return "Employee has not been Deleted";
}
catch
{
return "Record has not been Added";
}
The following figure shows one way to conceptualize the relationships between the
controller and context classes compared to not using the repository and unit of work
pattern at all.
Software design patterns are very important in the field of software engineering.
This is because design patterns give a software system scalability, testability,
reliability and maintainability. Design patterns are necessarily recurring solutions for
the recurring problems. A software engineer might have fall in difficulties while
finding some sort of viable solutions regarding a software system development. So,
if he/she picks a design pattern what is actually needed among the already invented
designed patterns then he/she would be able to save a lot of time by preventing
reinvention of the design pattern for the software system he/she is responsible for
to develop. Here, I would like to give some light on repository pattern and unit of
work pattern in a collective sense.
'Actual
'Actual
'Actual
'Actual
'Actual
'Actual
error
error
error
error
error
error
END CATCH
In the scope of the Catch block the error functions return error-related information
that you can reference in your T-SQL statements. Currently, SQL Server supports the
following functions for this purpose:
WORKING WITH TRY-CATCH BLOCK, ERROR FUNCTIONS AND @@TRANCOUNT BUILT IN FUNCTION
'Actual
'Actual
'Actual
'Actual
'Actual
'Actual
error
error
error
error
error
error
END CATCH
WORKING WITH TRY-CATCH BLOCK AND THROW STATEMENT INSTEAED OF ERROR FUNCTIONS
STEP 1:
BEGIN TRY
--Write necessary queries with/without transactions
END TRY
STEP 2:
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
THROW
END CATCH