Skip to content

Instantly share code, notes, and snippets.

@Fodsuk
Created June 30, 2012 18:56
Show Gist options
  • Save Fodsuk/3025099 to your computer and use it in GitHub Desktop.
Save Fodsuk/3025099 to your computer and use it in GitHub Desktop.
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
Copy link

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