Where should the Stored Proc business logic be placed in MVC?

I am looking for a bit of experience and explanation here, given that different sources give different recommendations. I am completely new to MVC. I know this question has been asked before, but I am not (currently) using EF or Linq.

I have a SQL database with many stored procedures. Previously, when used with webforms, there was a business layer that contained helper methods for calling procedures and returning DataSets to pages. The important part is that the procedures often questioned about 20 tables; the pages don't just reflect the structure of the database exactly (as I see in most MVC tutorials):

SQL database <--> stored procedures <--> business layer <--> web forms

      

I want to get the best approach here to start from the right position and learn the right way, but I admit that there can be no right answer. So if you are posting, could you please provide some explanation regarding the "why"?


  • Should the stored procedure logic (SQLCommand / business methods, etc.) go into the model or Controller?

One post does not report either , but keep the business layer. Another expert recommends that

[Models / entities] should not have any addon methods outside of what the database returns

  • If the business layer is saved, where is the method called from (for example, model or controller)?

  • If the above answer is "None", does that mean that part of the model will not be used?

It almost seems like things are not getting done properly, however in this tutorial that appears to be happening.

  • Do I have to wire the Entity Framework to the model layer to invoke the business layer?

It seems like overkill, adding all this extra logic.

+3


source to share


2 answers


Controllers must collect the information necessary to create the page the user is currently viewing. What is it.

Controllers must reference classes at the business logic level.

For example, here's your controller. All it does is translate the HTTP request and invoke the business logic.

public class MyController : Controller
{
   private IMyBusinessLogic  _businessLogic;
   public MyController(IMyBusinessLogic businessLogic)
   {
      _businessLogic = businessLogic;
   }

   [HttpPost]
   public ActionResult UpdateAllRecords()
   {
      _businessLogic.UpdateAllRecords();
      return Json(new Success());
   }
}

      

And your business logic class

public class MyBusinessLogic : IMyBusinessLogic  
{
  public void UpdateAllRecords()
  {
    // call SP here
    using(SqlConnection conn = new...
  }
}

      

There are a number of advantages for this:

  • Your business logic is completely decoupled from your user interface, there is no database code in your presentation layer. This means that your controller can focus on that task and your code doesn't get dirty.
  • You can check your controller and see what happens when your business logic succeeds, throws exceptions, etc.

For extra bonus points, you should look into creating a data access layer.

public void DataAccess : IDataAccess
{
  public void RunStoredProcedure(string spName)
  {
  }
}

      



Now you can check that your BLL is calling and processing your SP results correctly!

Expanded following a comment asking a question about models: Ideally, your model shouldn't have any logic at all. It should simply represent the data needed to create the page. The object you are loading is an object on the system, the model represents the data that is displayed on the page. This is often much easier and may contain additional information (such as their address) that are not present in the main entity, but appear on the page.

for example

public class Person
{
   public int  PersonID {get;set;}
   public string Firstname {get;set;}
   public string Lastname {get;set;}
   public Address Address {get;set;}
}

      

The model contains only the information you want to display:

public class PersonSummaryModel
{
   public int  PersonID {get;set;}
   public string FullName {get;set;}
}

      

Then you pass your model to your view to render it (possibly FullNames in this list of names). Many people need a mapping class to convert between the two, some do it in a controller.

for example

public class PersonMapper
{
  public PersonSummaryModel Map(Person person)
  {
     return new PersonSummaryModel
     {
       PersonID = person.PersonID,
       FullName = string.Concat(person.Firstname, " ", person.Lastname)
     };
  }
}

      

You can also use some automated solutions like AutoMapper to do this step for you.

+1


source


Your controller should only be involved to create the view structure. Create a separate class library called Data Access Layer or something more general, and create a class that handles calls to your stored processes, creating objects from results, etc. There are many opinions on how this should be handled, but perhaps the most

View
|
Controller
|
Business Logic
|
Data Access Layer
|--- SQL (Stored procs)
       -Tables
       -Views
       -etc.
|--- Alternate data sources
       -Web services
       -Text/XML files
       -and son on.

      



if you feel like learning level and best way MSDN has a great article at this MSDN link

+1


source







All Articles