HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

ASP.NET MVC to produce a repair invoice

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
repairmvcproducenetinvoiceasp

Problem

I just started working with ASP.NET MVC a few weeks ago, and I'm finding that it can be very easy to write spaghetti code in the controllers. For my first project, I created a very simple view with a few controls. At first, all of my code was in the Index() action result. That worked okay for a while, but as more features get added to the page, the more the code grows. I would like my code to be split up into multiple action results. I made an attempt at refactoring.

Here is my View:

```
@model TpgInternalSite.Models.RepairInvoice

@{
ViewBag.Title = "Repair Invoicing";
Layout = "~/Views/Shared/_Layout100.cshtml";
}

Repair Invoice

$(document).ready(function () {

$('@(ViewBag.SetFocusTo)').focus();

$('#RmaNumber, #SerialNumber').keydown(function (event) {
if (event.keyCode == 13 || event.keyCode == 9) {

var RmaNumber = $('#RmaNumber').val();
var SerialNumber = $('#SerialNumber').val();

event.preventDefault(); //Stops enter key from triggering the Process button.
if (event.target.id == 'RmaNumber'){

var link = '/Invoice/RmaNumberScan?rmaNumber=' + RmaNumber;
location.href = link;
}
else {

var link = '/Invoice/SerialNumberScan?rmaNumber=' + RmaNumber + '&serialNumber=' + SerialNumber;
location.href = link;
}
}
});
});

@using (Html.BeginForm("Index", "Invoice", FormMethod.Post))
{


RMA#


@Html.TextBoxFor(x => x.RmaNumber)




SERIAL#


@Html.TextBoxFor(x => x.SerialNumber)




Terminal Type:


@Html.LabelForModel(Model.Termi

Solution

It sounds like you need to add a service layer where you can include all your business logic. This way your controller classes do not become bloated with business logic and they are only dealing with the handling of requests and the population of view-models.

Using thin controllers like this you can separate your logic out to different services and make it easier to practice the Single Responsibility Principle.

Please see this question and accepted answer but I've included an example below: Creating a service layer for my MVC application

Your controller:

[Dependency]
public IInvoiceService InvoiceService;

public ActionResult Index()
{
    //code
    return View(repairInvoice);
}

[HttpPost]
public ActionResult Index(RepairInvoice ri)
{ 
    this.InvoiceService.ProcessInvoice(ri.RmaNumber, ri.SerialNumber); // add on other params as appropriate
    return View(ri);
}


Service layer would consist of multiple classes, each with their own responsibility such as:

public class InvoiceService : IInvoiceService
{
    public void ProcessInvoice(string rmaNumber, string serialNumber) 
    {
        // Do any processing here
    }
}


I would recommend creating an interface for each separate service and then you can use Dependency Injection to instantiate the concrete classes using Unity or similar.

Code Snippets

[Dependency]
public IInvoiceService InvoiceService;

public ActionResult Index()
{
    //code
    return View(repairInvoice);
}

[HttpPost]
public ActionResult Index(RepairInvoice ri)
{ 
    this.InvoiceService.ProcessInvoice(ri.RmaNumber, ri.SerialNumber); // add on other params as appropriate
    return View(ri);
}
public class InvoiceService : IInvoiceService
{
    public void ProcessInvoice(string rmaNumber, string serialNumber) 
    {
        // Do any processing here
    }
}

Context

StackExchange Code Review Q#36061, answer score: 7

Revisions (0)

No revisions yet.