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?
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);