是或否:MVC中的模型是否包含应用程序逻辑? [英] Yes or no: Should models in MVC contain application logic?

查看:80
本文介绍了是或否:MVC中的模型是否包含应用程序逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我和我们的开发者就MVC进行了一些讨论,更准确地说就是模型组件在MVC中的作用。



在我看来,一个模型应该只是包含属性和几乎没有功能,所以尽可能少的模型类中的方法。



我的同事虽然认为模型可以而且应该有更多的,并提供一个更多的功能。



这是一个我们争论的例子。



示例1



假设我们想创建一个博客。博客有文章和标签。每篇文章都可以有多个标签,每个标签可以属于多个文章。所以我们有这个关系:



在伪代码中,它可能看起来像这样:

  class Article {
public int id;
public String title;
public String content;
public Tag []标签;

//构造函数
public void Article(id,title,content,tags){
this.id = id;
this.title = title;
this.content = content;
this.tags = tags;
}
}

class标签{
public int id;
public String name;

//构造函数
public标签(id,name){
this.id = id;
this.name = name;
}
}

现在,假设我们正在松动耦合这意味着我们有一个没有标签的文章的实例,所以我们将使用一个Ajax调用(对我们的后端具有包含所有信息的数据库)来获取属于我们文章的标签。 / p>

这里是棘手的部分。我相信通过Ajax + JSON获取后端数据应该是控制器的工作,使用一个专门的类来处理使用解析器的ajax请求:

  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()); //请求后端通过JSON获取文章
文章article = json2Article(json);

//为了防止
Tag [] tags = article.tags;
if(tags == null || tags.length< = 0){
json = AjaxUtil.getContent(article.id,ContentType.TAGS); //从后端通过ajax
标签= json2Tags(json)获取此文章的所有标签;
article.tags = tags;
}

返回文章;
}

//时髦的魔法和解析JSON字符串。然后创建一个新的实例文章
public static article json2Article(String json){
/ *
...
* /
返回新文章(id,title ,内容,标签);
}

//时髦的魔法和解析JSON字符串。然后创建一个新的Tag
实例public static Tag [] json2Tags(String json){
/ *
...
* /
return tags;
}

}

示例2



我的同事认为这违反了MVC的想法,他建议该模型应该关注这一点:

  class Blog {
public int id;
public String title;
public Article [] articles;

//构造函数
public Blog(id,title,articles){
this.id = id;
this.title = title;
this.articles =文章;
}

public void getArticles(){
if(articles == null || articles.length< = 0){
String json = AjaxUtil.getContent (id,ContentType.ARTICLE); //从后端通过ajax
article = json2Articles(json)获取此博客的所有文章;
}
返回文章;
}

私人文章[] json2Articles(String json){
/ *
...
* /
返回文章;
}

}

class文章{
public int id;
public String title;
public String content;
public Tag []标签;

//构造函数
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); //从后端通过ajax
tags = json2Tags获取此文章的所有标签;
}
返回标签;
}

//时髦的魔法和解析JSON字符串。然后创建一个新的Tag
实例私有Tag [] json2Tags(String json){
/ *
...
* /
返回标签;
}
}

在您要做的模型之外: code> blog.getArticles(); 或 article.getTags(); 来获取标签,而不用担心ajax调用。 / p>

然而,由于这可能是相当方便的,我认为这种方法打破了MVC,因为在最后的一天,所有的模型将充满各种各样的时髦的东西和方法,控制器和帮助类几乎没有任何东西。



在我对MVC的理解中,模型应该只包含属性和最小的帮助方法。例如,一个模型文章可以提供一个方法getNumOfTags(),但它不应该自己做任何Ajax调用。



那么哪种方法是正确的? / p>

解决方案

一般来说,我试图让控制器在逻辑上也很简单。如果需要业务逻辑,那么它将会升级到服务层类来处理它。这也可以节省重复任何代码/逻辑,如果业务逻辑要改变,最终会使整个项目更易于维护。我只是将模型纯粹作为实体对象。



我认为上面的答案很好地总结出来,很容易根据设计模式过度设计一个项目:无论对你而言都是有效的,最可维护/最有效率的。


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?

解决方案

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.

这篇关于是或否:MVC中的模型是否包含应用程序逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆