Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4338884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:08:30+00:00 2026-05-21T11:08:30+00:00

I was just wondering how much logic you put in your controllers? I know

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:08:31+00:00Added an answer on May 21, 2026 at 11:08 am

    Only the AddItemToCart and UpdateCartItem actions 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just wondering how much of a performance will you lose by using
Just wondering... Does it? And how much Like including 20 .php files whith classes
How much of this is auto-generated methods in Rails User.find_or_create_from_auth_hash(auth_hash). That is just wondering
I don't know yahoo pipes so much, i am just wandering, is it possible
I'm just wondering how people working in teams manage the relationship between the logic
In the program I have pasted below, I was just wondering why the pointer
I have not seen much support for Grails to develop facebook apps.I was just
I was just wondering if someone can help with an annoyance I have with
just wondering if there is a way to reduce the amount of code needed
Just wondering: I'm trying to set up an adaptive image handler in Coldfusion8, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.