Created
June 30, 2012 18:56
-
-
Save Fodsuk/3025099 to your computer and use it in GitHub Desktop.
service layer exampe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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