Yesterday I had some discussion with one of our developers regarding MVC, more precisely about the role of the model component in MVC.
In my opinion, a model should just contain properties and almost no functionality so there are as few methods in model classes as possible.
My collegue though believes that models could and should have more than that and offer a lot more functionality.
Here is an example we argued about.
Example 1
Let's say we wanted to create a blog. A blog has articles and tags. Each article can have multiple tags and each tag can belong to multiple articles. So we have a m:n relation here.
In pseudocode it'd probably look something like this:
class Article{ public int id; public String title; public String content; public Tag[] tags; // Constructor public void Article(id, title, content, tags){ this.id = id; this.title = title; this.content = content; this.tags = tags; } } class Tag{ public int id; public String name; // Constructor public Tag(id, name){ this.id = id; this.name = name; } }
Now, assume that we're working loose coupled here which means that it could happen that we have an instance of Article which has no Tags yet so we'll use an Ajax call (to our backend which has a database containing all the information) to get the tags that belong to our article.
Here comes the tricky part. I believe that getting the backend data via Ajax+JSON should be the controller's job using a dedicated class which deals with the ajax request using a parser:
class MyController{ private void whatever(articleID){ Article article = (Article) ContentParser.get(articleID, ContentType.ARTICLE); doSomethingWith(article); } } public abstract class ContentParser{ public static Object get(int id, ContentType type){ String json = AjaxUtil.getContent(id, type.toString()); // Asks the backend to get the article via JSON Article article = json2Article(json); // Just in case Tag[] tags = article.tags; if (tags == null || tags.length <= 0){ json = AjaxUtil.getContent(article.id, ContentType.TAGS); // Gets all tags for this article from backend via ajax tags = json2Tags(json); article.tags = tags; } return article; } // Does funky magic and parses the JSON string. Then creates a new instance of Article public static Article json2Article(String json){ /* ... */ return new Article(id, title, content, tags); } // Does funky magic and parses the JSON string. Then creates a new instance of Tag public static Tag[] json2Tags(String json){ /* ... */ return tags; } }
Example 2
My collegue believes that this breaks with the idea of MVC, he suggests that the model should take care about this:
class Blog{ public int id; public String title; public Article[] articles; // Constructor public Blog(id, title, articles){ this.id = id; this.title = title; this.articles = articles; } public void getArticles(){ if (articles == null || articles.length <= 0){ String json = AjaxUtil.getContent(id, ContentType.ARTICLE); // Gets all articles for this blog from backend via ajax articles = json2Articles(json); } return articles; } private Article[] json2Articles(String json){ /* ... */ return articles; } } class Article{ public int id; public String title; public String content; public Tag[] tags; // Constructor public Article(id, title, content, tags){ this.title = title; this.content = content; this.tags = tags; } public Tag[] getTags(){ if (tags == null || tags.length <= 0){ String json = AjaxUtil.getContent(id, ContentType.TAGS); // Gets all tags for this article from backend via ajax tags = json2Tags; } return tags; } // Does funky magic and parses the JSON string. Then creates a new instance of Tag private Tag[] json2Tags(String json){ /* ... */ return tags; } }
And outside of the model you'd do: blog.getArticles();
or article.getTags();
to get the tags without bothering with the ajax call.
However, as handy as this might be I believe that this approach breaks with MVC because at the end of the day all models will be full of methods that do various funky stuff and the controller and helper classes do almost nothing.
In my understanding of MVC, models should only contain properties and a minimum of "helper methods" inside. For example a model "Article" could offer a method getNumOfTags() but it shouldn't do any Ajax calls on its own.
So, which approach is correct?
The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application.
Models should contain logic that deals with manipulating data. For example, say we have a post model, the model should contain methods that get the comments of a particular post. In Sails models, each model has static methods like find , findOne , update , etc.
Adding a Model Class In the MVC application in Visual Studio, and right-click on the Model folder, select Add -> and click on Class... It will open the Add New Item dialog box. In the Add New Item dialog box, enter the class name Student and click Add. This will add a new Student class in model folder.
A1: Business Logic goes to Model part in MVC . Role of Model is to contain data and business logic. Controller on the other hand is responsible to receive user input and decide what to do.
Generally I try to keep controllers simple in terms of logic too. If business logic is required, it will go up to 'service layer' classes to handle it. This also saves repeating any code/logic too, which ultimately makes the whole project more maintainable if business logic was to change. I just keep models purely as entity objects.
I think the answer above sums it up nicely though, it is easy to over engineer a project based on design patterns: Go with whatever works for you and is most maintainable/efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With