Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Business Logic Organization [closed]

I'm trying to learn MVC with ASP.Net and am reading Steve Sanderson's book. One thing I'm confused about is where to place the business logic?

For example, when deleting a product all Sanderson has is a method in his CartController that calls the Delete method on the productsRepository. This is strange to me because if there were any business logic, such as ensuring that the product isn't in anyone's shopping cart first, etc. it would have to either be in the products repository or the CartController.

Both of these seem like bad places to put business logic; the products repository is meant to be easily replaced with another (switching from using a db to using a session) and using the Controller means you are putting the business logic in the UI layer.

Shouldn't he be using a class that contains the business logic and calls the repository's delete method instead? The repository being a member variable of the business logic class'?

like image 727
user660734 Avatar asked Apr 02 '11 19:04

user660734


People also ask

What is MVC business logic?

MVC enables the application to be extensible and modular by separating the application into three parts: the business logic part, which implements data retrieval and manipulation. the user interface part, which is what the application users see. the controller part, which routes requests to the proper objects.

Where do you put your business logic typically?

Business logic should live in the data model. And, what's more, it should live in the graph data model because that's the right abstraction for the next twenty years.

Does bridge MS contains any business logic?

The Bridge Framework is an integration framework deployed on SAP BTP that allows developers to quickly customize and deploy apps that bring the powerful business logic of SAP products to the convenience of Microsoft Teams.


1 Answers

I typically structure my MVC solutions in a way resembling the following:

  • X.Core
    • general extension methods, logging, and other non-web infrastructure code
  • X.Domain
    • domain entities and repositories
  • X.Domain.Services
    • domain services for orchestrating complex domain operations, such as adding a product to a shopping cart
  • X.Application.Core
    • application logic (initialization (route registration, IoC configuration, etc.), web-specific extensions, MVC filters, controller base classes, view engines, etc.)
  • X.Application.Models
    • view model classes
  • X.Application.Services
    • service classes that can return ViewModels by accessing repositories or domain services, as well as the other way around for updates
  • X.Application.Web
    • controllers, views and static resources

Some of these could be combined, but having them separate makes it easier to locate stuff and to ensure your layer boundaries are respected.

A typical controller action for showing the product cart might look like this:

public virtual ActionResult ProductCart()
{
    var applicationService = <obtain or create appropriate service instance>
    var userID = <obtain user ID or similar from session state>
    var viewModel = applicationService.GetProductCartModel( userID );
    return View( "Cart", viewModel );
}

A typical controller action for adding a product to the shopping cart might thus look something like this:

public virtual ActionResult AddProductToCart( int productID )
{
    var domainService = <obtain or create appropriate service instance>
    var userID = <obtain user ID or similar from session state>
    var response = domainService.AddProductToCart( userID, productID );
    return Json( new { Success = response.Success, Message = response.Message } );
}
like image 55
Morten Mertner Avatar answered Sep 21 '22 03:09

Morten Mertner