Difference between revisions of "Unit Of Work Pattern"

From Logic Wiki
Jump to: navigation, search
Line 11: Line 11:
 
using System;
 
using System;
  
namespace Monky.API.Core.Data.Interfaces
+
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; }
         IPersonRepository UserMenus { get; }
+
         IPayeeRepository Payee { get; }
 
         int Complete();
 
         int Complete();
 
     }
 
     }
Line 28: Line 28:
 
=== Implementation ===
 
=== Implementation ===
 
<pre class="brush:csharp;">
 
<pre class="brush:csharp;">
using System;
+
using Spv.Api.Core.IDataAccess;
using System.Collections.Generic;
+
using System.Linq;
+
using System.Threading.Tasks;
+
using Monky.API.Core.Data.Interfaces;
+
using Monky.API.Core.Data.Repositories;
+
  
namespace Monky.API.Core.Data
+
namespace Spv.Api.Core.DataAccess
 
{
 
{
 
     public class UnitOfWork : IUnitOfWork
 
     public class UnitOfWork : IUnitOfWork
 
     {
 
     {
         private readonly MonkyDbContext _context;
+
         private readonly SpvDbContext _context;
  
         public UnitOfWork(MonkyDbContext context)
+
         public UnitOfWork(SpvDbContext context)
 
         {
 
         {
 
             _context = context;
 
             _context = context;
             UserMenus = new PersonRepository(_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 IPersonRepository UserMenus { get; }
 
        // ...
 
  
 
         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


Usage

UnitOfWork.JPG

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