Skip to content

Instantly share code, notes, and snippets.

@GFoley83
Forked from Fodsuk/gist:3025099
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GFoley83/ac35f56ec4bf20926f5f to your computer and use it in GitHub Desktop.
Save GFoley83/ac35f56ec4bf20926f5f to your computer and use it in GitHub Desktop.
public class InventoryController : ApiController
{
private readonly IInventoryManagementService _inventoryManagementService;
private readonly IUnitOfWork _unitOfWork;
public InventoryController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork; //access services
_inventoryManagementService = _unitOfWork.Get<IInventoryManagementService>();
}
//thin controller action
public ActionResult UpdateItem(InventoryItemViewModel item)
{
InventoryItem domainItem = item.ToDomainObject();
_inventoryManagementService.AddOrUpdateItem(domainItem);
return View();
}
}
public class InventoryManagementService : IInventoryManagementService
{
IInventoryRepository _inventoryRepository;
IUserRepository _userRepository;
//inject any repositories that are require or provide repositories via a UnitOfWork
public InventoryManagementService(IInventoryRepository inventoryRepository, IUserRepository userRepository)
{
_inventoryRepository = inventoryRepository;
_userRepository = userRepository;
}
// Business logic is in the service not the controller action
public void AddOrUpdateItem(InventoryItem item)
{
var itemExists = _inventoryRepository.GetById(item.Id);
if (itemExists)
{
_inventoryRepository.Update(item);
}
else
{
_inventoryRepository.Add(item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment