LP13
LP13

Reputation: 34109

In MVC where to perform calculations when data is coming from UI?

I have been reading about where to put business logic in ASP.NET MVC Project and I still can't get clear on some things. Especially performing the calculations or business logic when the data is coming from UI.

I have ASP.NET MVC application using EF. The application is typical 3 tier application.

    UI  - has View Models
    Domain (aka Service Layer) - has DTO classes
    DAL  - has EF entities

Each layer has its own C# classes used for transferring data between the layers.
UI has reference to Domain and Domain has reference to DAL
The business logic of the application is in Domain (service layer)

Typically any kind of business logic/calculations are done in service layer. And that is correct when the data is coming from the database.

But what if the data is coming from UI?

Lets say the view is already rendered with several fields. There is corresponding view model in UI layer. A user update some fields and click a button, which makes a ajax POST to controller action. So server now has populated view model that is coming from UI. Now i want to do some calculations and return json data.

 [HttpGet]
 public ActionResult Index(int id)
 {
   MyViewModel model = _service.GetModel(id);
   return View(model);
 }

 [HttpPost]
 public ActionResult Calculate(MyViewModel model)
 {
     // preform calculations but where??
     var differentmodel = PerformCalculations(model);
     return Json(someDifferentmodel);
 }

Since Domain Layer cannot have access to View Models i can not pass MyViewModel to Domain and do the calculation there.
One option i see here like below

 public ActionResult Calculate(MyViewModel model)
 {
     1> Convert MyViewModel to DTO
     2> pass DTO to service layer for calculations
     3> get calculated DTO from service layer. 
     4> Convert DTO to differentViewModel

      return Json(differentViewModel);
 }

There is lot of back & forth mapping, which seems unnecessary.

Is there any better approach?
In terms of design practice, Is it okay to introduce a new layer between UI and Domain that has reference to both UI and Domain. This new layer will be responsible to do the business logic/calculations. In-fact then i can use this layer for calculations regardless the data is coming from UI or domain. What would be this layer? (I know technically i can do that but i am asking is it recommended design practice)

Upvotes: 1

Views: 920

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239290

You're making this unnecessarily complex. The first question to ask is: "Is this calculation logic relevant only to the view or is it applicable in other places, such as the DAL? If it's only relevant to the view, put it on your view model. If it has broader applicability then put it in a class library or something that is reference by all projects that need it. In that latter case, you'd likely just create a helper class that does the calculations, and you'd pass in only the relevant data to make those calculations.

Then, in your UI layer, you can either utilize the view model or another helper class to specifically handle the view model. For example:

Class Library

public static class CalculationHelper
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

UI

public class MyViewModel
{
    ...

    public FooBarSum
    {
        get { return CalculationsHelper.Add(Foo, Bar); }
    }
}

OR

public static class UICalculationsHelper
{
    public static int Add(MyViewModel model)
    {
        return CalculationsHelper.Add(model.Foo, model.Bar);
    }
}

Upvotes: 1

Related Questions