Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
service layer exampe
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);
}
}
}
@RyanAdriano

This comment has been minimized.

Copy link

commented Feb 9, 2013

Hi! Can you tell me how you implemented this

_unitOfWork.Get< IInventoryManagementService >();

I guess I just don't get how an interface variable can do this;

_inventoryManagementService.AddOrUpdateItem(domainItem);

Thank you,

Ryan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.