ASP.NET MVC控制器逻辑 - 这太多了吗?

时间:2011-04-17 20:25:36

标签: asp.net-mvc

我只是想知道你在控制器中放了多少逻辑?我知道他们应该是苗条的,但似乎你必须加入一些。

请看看我的控制器,让我知道你会做什么来折射它。谢谢!

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

更新

所以我重构了一些建议(谢谢你们!)。这就是我想出的:

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

2 个答案:

答案 0 :(得分:1)

只有AddItemToCartUpdateCartItem操作才需要重构。我会将这两种方法包含的业务逻辑移动到服务层中。控制器中的私有方法也总是很糟糕。

答案 1 :(得分:1)

修身并不意味着空洞! ;-)以下部分在我看来“闻起来”:

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

我会把它移到ShoppingCard。看起来违反了“告诉不要问”。

相关问题