Difference between revisions of "Unit Of Work Pattern"
From Logic Wiki
| Line 11: | Line 11: | ||
using System; | using System; | ||
| − | namespace | + | namespace Spv.Api.Core.IDataAccess |
{ | { | ||
public interface IUnitOfWork : IDisposable | public interface IUnitOfWork : IDisposable | ||
| Line 19: | Line 19: | ||
// properties below in unit tests | // properties below in unit tests | ||
| − | + | IDealRepository Deal { get; } | |
| − | + | IPayeeRepository Payee { get; } | |
int Complete(); | int Complete(); | ||
} | } | ||
| Line 28: | Line 28: | ||
=== Implementation === | === Implementation === | ||
<pre class="brush:csharp;"> | <pre class="brush:csharp;"> | ||
| − | using | + | using Spv.Api.Core.IDataAccess; |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | namespace | + | namespace Spv.Api.Core.DataAccess |
{ | { | ||
public class UnitOfWork : IUnitOfWork | public class UnitOfWork : IUnitOfWork | ||
{ | { | ||
| − | private readonly | + | private readonly SpvDbContext _context; |
| − | public UnitOfWork( | + | public UnitOfWork(SpvDbContext context) |
{ | { | ||
_context = context; | _context = context; | ||
| − | + | Payee = new PayeeRepository(_context); | |
| − | + | Deal = new DealRepository(_context); | |
} | } | ||
| + | |||
| + | |||
| + | public IPayeeRepository Payee { get; private set; } | ||
| + | public IDealRepository Deal { get; private set; } | ||
public void Dispose() | public void Dispose() | ||
| Line 52: | Line 51: | ||
_context.Dispose(); | _context.Dispose(); | ||
} | } | ||
| − | |||
| − | |||
| − | |||
public int Complete() | public int Complete() | ||
| Line 64: | Line 60: | ||
</pre> | </pre> | ||
| + | === Usage === | ||
| + | <pre class="brush:csharp;"> | ||
| + | using System.Web.Mvc; | ||
| + | using Spv.Api.Core.IDataAccess; | ||
| + | |||
| + | namespace Spv.Api.Controllers | ||
| + | { | ||
| + | public class HomeController : Controller | ||
| + | { | ||
| + | private readonly IUnitOfWork _unitOfWork; | ||
| + | |||
| + | public HomeController(IUnitOfWork unitOfWork) | ||
| + | { | ||
| + | _unitOfWork = unitOfWork; | ||
| + | } | ||
| + | public ActionResult Index() | ||
| + | { | ||
| + | ViewBag.Title = "Home Page"; | ||
| + | ViewBag.Temp = _unitOfWork.Payee.Get(1).Name; | ||
| + | return View(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </pre> | ||
See Also : [[Repository Pattern]] | See Also : [[Repository Pattern]] | ||
Revision as of 10:46, 6 June 2017
Contents
Usage
Interface
using System;
namespace Spv.Api.Core.IDataAccess
{
public interface IUnitOfWork : IDisposable
{
// for testability we user interfaces below.
// we can mock IUnitWork interface and the
// properties below in unit tests
IDealRepository Deal { get; }
IPayeeRepository Payee { get; }
int Complete();
}
}
Implementation
using Spv.Api.Core.IDataAccess;
namespace Spv.Api.Core.DataAccess
{
public class UnitOfWork : IUnitOfWork
{
private readonly SpvDbContext _context;
public UnitOfWork(SpvDbContext context)
{
_context = context;
Payee = new PayeeRepository(_context);
Deal = new DealRepository(_context);
}
public IPayeeRepository Payee { get; private set; }
public IDealRepository Deal { get; private set; }
public void Dispose()
{
_context.Dispose();
}
public int Complete()
{
return _context.SaveChanges();
}
}
}
Usage
using System.Web.Mvc;
using Spv.Api.Core.IDataAccess;
namespace Spv.Api.Controllers
{
public class HomeController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HomeController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public ActionResult Index()
{
ViewBag.Title = "Home Page";
ViewBag.Temp = _unitOfWork.Payee.Get(1).Name;
return View();
}
}
}
See Also : Repository Pattern