I was just wondering how much logic you put in your controllers? I know they are supposed to be slim, but it seems you have to put in some.
Have a look at my controller please and let me know what you would do to refractor it. Thanks!!
Namespace Controllers
Public Class ShopController
Inherits ControllerBase
#Region "Members/Properties"
Private ProductService As IProductService
Private UnitOfWork As IUnitOfWork
#End Region
#Region "Constructor(s)"
Public Sub New(ProductService As IProductService, UnitOfWork As IUnitOfWork)
Me.ProductService = ProductService
Me.UnitOfWork = UnitOfWork
End Sub
#End Region
#Region "Methods"
<HttpGet()>
Function Index() As ActionResult
Return View(New ShopViewModel With {
.Categories = Mapper.Map(Of IEnumerable(Of Category), IEnumerable(Of CategoryViewModel))(ProductService.GetCategories)
})
End Function
<HttpGet()>
Function ListProducts(ID As String, CatID As Integer) As ActionResult
Return View(New ShopViewModel With {
.Products = Mapper.Map(Of IEnumerable(Of Product), IEnumerable(Of ProductViewModel))(ProductService.GetProductsByCategoryID(CatID))
})
End Function
<HttpPost()>
Function GetCartAsJson() As ActionResult
Return New JsonResult With {.Data = ShoppingCart}
End Function
<HttpPost()>
Function AddItemToCart(Model As ShoppingCartItemViewModel) As ActionResult
Dim Item As ShoppingCartItem
Item = ShoppingCart.GetItemBySku(Model.Sku)
If (Item IsNot Nothing) Then
Item.Quantity += Model.Quantity
UpdateCartItemPricing(Item)
Else
Dim Product = ProductService.GetProductDetailBySku(Model.Sku)
Item = New ShoppingCartItem With {
.ProductID = Product.ID,
.Sku = Model.Sku,
.Name = Product.Name,
.Description = Product.ChildProducts(0).Name,
.Price = 0D,
.Quantity = Model.Quantity
}
ShoppingCart.AddItem(Item)
UpdateCartItemPricing(Item)
End If
Return New JsonResult With {.Data = ShoppingCart}
End Function
<HttpPost()>
Function UpdateCartItem(Model As ShoppingCartItemViewModel) As ActionResult
Dim Item = ShoppingCart.GetItemBySku(Model.Sku)
If (Item IsNot Nothing) Then
If (Model.Quantity < 1) Then
ShoppingCart.DeleteItem(Item)
Else
Item.Quantity = Model.Quantity
UpdateCartItemPricing(Item)
End If
End If
Return New JsonResult With {.Data = ShoppingCart}
End Function
Private Sub UpdateCartPricing()
For Each Item In ShoppingCart.Items
UpdateCartItemPricing(Item)
Next
End Sub
Private Sub UpdateCartItemPricing(Item As ShoppingCartItem)
Item.Price = ProductService.GetPriceForSkuByQuantity(Item.Sku, Item.Quantity)
End Sub
#End Region
End Class
End Namespace
Update
So I refactored it one some suggestions (thanks guys!). This is what I came up with:
Namespace Controllers
Public Class ShopController
Inherits ControllerBase
#Region "Members/Properties"
Private ProductService As IProductService
Private ShoppingCartService As IShoppingCartService
Private UnitOfWork As IUnitOfWork
#End Region
#Region "Constructor(s)"
Public Sub New(ProductService As IProductService, ShoppingCartService As IShoppingCartService, UnitOfWork As IUnitOfWork)
Me.ProductService = ProductService
Me.ShoppingCartService = ShoppingCartService
Me.UnitOfWork = UnitOfWork
End Sub
#End Region
#Region "Methods"
<HttpGet()>
Function Index() As ActionResult
Return View(New ShopViewModel With {
.Categories = Mapper.Map(Of IEnumerable(Of Category), IEnumerable(Of CategoryViewModel))(ProductService.GetCategories)
})
End Function
<HttpGet()>
Function ListProducts(ID As String, CatID As Integer) As ActionResult
Return View(New ShopViewModel With {
.Products = Mapper.Map(Of IEnumerable(Of Product), IEnumerable(Of ProductViewModel))(ProductService.GetProductsByCategoryID(CatID))
})
End Function
<HttpPost()>
Function GetCartAsJson() As ActionResult
Return New JsonResult With {.Data = ShoppingCart}
End Function
<HttpPost()>
Function AddItemToCart(Model As ShoppingCartItemViewModel) As ActionResult
ShoppingCartService.AddItem(ShoppingCart, Model.Sku, Model.Quantity)
Return New JsonResult With {.Data = ShoppingCart}
End Function
<HttpPost()>
Function UpdateCartItem(Model As ShoppingCartItemViewModel) As ActionResult
ShoppingCartService.UpdateItemQuantity(ShoppingCart, Model.Sku, Model.Quantity)
Return New JsonResult With {.Data = ShoppingCart}
End Function
#End Region
End Class
End Namespace
Only the
AddItemToCartandUpdateCartItemactions require refactoring. I would move the business logic those two methods contain into the service layer. Also private methods in a controller are always stinky.