2

I'm pretty clear business logic goes into Models. But on this question, they refer to business logic as pretty much low level validation (if the user signs up, ask for their e-mail).

What about business logic that goes beyond validation? For example, right now I'm working on a project where the user is able to generate a quotation after a form submission. Once submitted, the application will process several data (that are not related to said form) and create some more database rows. This process involves 3 different model entities, which clearly indicates placing the code for this process inside any of those model entity classes would be "wrong".

Right now I'm using Unit of Work pattern to encapsulate CRUD operations (database and mock for testing), but I have somewhere read about some called Service pattern, that encapsulates said UnitOfWork and possibly processes like described above.

What's the pattern used for this kind of logic?

  • Do you need to know how to implement your solution or are you just asking for its pattern name? – JeffO Feb 27 '15 at 13:42
  • The more detailed the answer, the more I'd understand how to encapsulate this logic and where – Christopher Francisco Feb 27 '15 at 13:47
2

It still belongs in a model, just not a view model. Since MVC assumes you are working in the UI they omit a "V"... View-Model View Controller, VMVC does not have the same ring to it.

If your application is trivial, your view model and business model can be the same thing, and that's okay. But it sounds like you application is past that point.

public class Quote
{
   public string Prerequisite1 { get; set; }
   public string Prerequisite2 { get; set; }
   public string GenerateQuote()
   {
      //Logics
   }
}

//Controller:
Quote quote = new Quote();
quote.Prerequisite1 = viewModel.TextField1;
quote.Prerequisite2 = repository.GetFromDB();
string shownToUser = quote.GenerateQuote();
repository.SaveWhatYouNeedToSave(quote);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.