Suppose I have an entity like Student and using MVC pattern I make a Student Model in my project. But there are certain times when I create a table I get foreign key of another entity. (One to many relationship - Student Borrow Book) So when I create Book Model I have to include foreign key as an attribute of the Book class. So does it violate the MVC pattern? Please help. How can I implement this using MVC ?
假设我有一个像Student这样的实体并且使用MVC模式我在我的项目中创建了一个学生模型。但有一些时候,我创建一个表,我得到另一个实体的外键。 (一对多的关系 - 学生借书)所以当我创建Book Model时,我必须包含外键作为Book类的属性。那么它是否违反了MVC模式?请帮忙。如何使用MVC实现这一点?
2 个解决方案
#1
2
No, It does not violate MVC.
不,它不违反MVC。
By definition, "Model" in MVC means logical representation of data in application. Building relationship between entity(in your case, Student and Book) is a way to represent student's information.
根据定义,MVC中的“模型”表示应用程序中数据的逻辑表示。建立实体之间的关系(在您的情况下,学生和书籍)是一种表示学生信息的方式。
Say, I am building a web application on "School Library".
说,我正在“学校图书馆”上建立一个网络应用程序。
- Entity(Student and Book) relationship will be all part of "model".
- Now, there is web portal (or simply a web page) where student can check book he has borrowed, is a "view".
- Classes(logic written to fetch data from model and render it to view) that connect Model and View are written in "Controller".
实体(学生和书籍)关系将成为“模范”的一部分。
现在,有一个门户网站(或简称一个网页),学生可以查看他借来的书,是一个“视图”。
连接Model和View的类(用于从模型获取数据并写入视图的逻辑)写在“Controller”中。
Check web framework like Django, Laravel. They follow MVC paradigm to build complex web applications.
检查像Django,Laravel这样的Web框架。他们遵循MVC范例来构建复杂的Web应用程序。
Hope it helps.
希望能帮助到你。
#2
1
Let's back up. "MVC" is "Model-View-Controller"; each of the three is a distinct part. The model, the subject of your question, is the part that describes the world of your program. The model comprises the nouns, adjectives, and verbs of your problem space, or "domain of discourse". Only use nouns, adjectives, and verbs that belong to the domain. That structure of domain-relevant types that represent the actors or entities (nouns) with their attributes (nouns and adjectives), and behaviors (verbs) is your model.
让我们回来吧。 “MVC”是“模型 - 视图 - 控制器”;三者中的每一个都是不同的部分。模型是问题的主题,是描述程序世界的部分。该模型包括问题空间的名词,形容词和动词,或“话语领域”。仅使用属于该域的名词,形容词和动词。表示具有属性(名词和形容词)和行为(动词)的演员或实体(名词)的领域相关类型的结构是您的模型。
In your domain you mentioned two nouns and a verb. "Student borrows Book". Now I've never met a student who had a foreign key. (Unless it was to their exchange-student girl friend's apartment.) Nor a book. So "foreign key" is not a part of the model.
在你的域名中,你提到了两个名词和一个动词。 “学生借书”。现在我从未见过有外国钥匙的学生。 (除非是交换学生女朋友的公寓。)也不是书。所以“外键”不是模型的一部分。
Here's a model:
这是一个模型:
public interface Person {
String getName();
}
public interface Borrower<T> {
void borrow(T thing);
}
public interface Replacer<T> {
void replace (T thing);
}
public class Student implements Person,
Borrower<Book>, Replacer<Book> {
private final Set<Book> bookshelf = new HashSet<>();
public Collection<Book> getBookshelf() {
return Collections.unmodifiableSet(bookshelf);
}
// more implementation
}
I purposely decomposed the model into single-abstract-method (SAM) interfaces. The Person
interface would not be one typically, though. The action methods, transitive verbs like "borrow" and "replace", should be SAM interfaces, in part to support lambdas, in part to keep your model clear and simple to work with.
我故意将模型分解为单抽象方法(SAM)接口。但是Person接口通常不是一个。动作方法,像“借用”和“替换”这样的及物动词应该是SAM接口,部分是为了支持lambda,部分是为了让你的模型清晰易懂。
#1
2
No, It does not violate MVC.
不,它不违反MVC。
By definition, "Model" in MVC means logical representation of data in application. Building relationship between entity(in your case, Student and Book) is a way to represent student's information.
根据定义,MVC中的“模型”表示应用程序中数据的逻辑表示。建立实体之间的关系(在您的情况下,学生和书籍)是一种表示学生信息的方式。
Say, I am building a web application on "School Library".
说,我正在“学校图书馆”上建立一个网络应用程序。
- Entity(Student and Book) relationship will be all part of "model".
- Now, there is web portal (or simply a web page) where student can check book he has borrowed, is a "view".
- Classes(logic written to fetch data from model and render it to view) that connect Model and View are written in "Controller".
实体(学生和书籍)关系将成为“模范”的一部分。
现在,有一个门户网站(或简称一个网页),学生可以查看他借来的书,是一个“视图”。
连接Model和View的类(用于从模型获取数据并写入视图的逻辑)写在“Controller”中。
Check web framework like Django, Laravel. They follow MVC paradigm to build complex web applications.
检查像Django,Laravel这样的Web框架。他们遵循MVC范例来构建复杂的Web应用程序。
Hope it helps.
希望能帮助到你。
#2
1
Let's back up. "MVC" is "Model-View-Controller"; each of the three is a distinct part. The model, the subject of your question, is the part that describes the world of your program. The model comprises the nouns, adjectives, and verbs of your problem space, or "domain of discourse". Only use nouns, adjectives, and verbs that belong to the domain. That structure of domain-relevant types that represent the actors or entities (nouns) with their attributes (nouns and adjectives), and behaviors (verbs) is your model.
让我们回来吧。 “MVC”是“模型 - 视图 - 控制器”;三者中的每一个都是不同的部分。模型是问题的主题,是描述程序世界的部分。该模型包括问题空间的名词,形容词和动词,或“话语领域”。仅使用属于该域的名词,形容词和动词。表示具有属性(名词和形容词)和行为(动词)的演员或实体(名词)的领域相关类型的结构是您的模型。
In your domain you mentioned two nouns and a verb. "Student borrows Book". Now I've never met a student who had a foreign key. (Unless it was to their exchange-student girl friend's apartment.) Nor a book. So "foreign key" is not a part of the model.
在你的域名中,你提到了两个名词和一个动词。 “学生借书”。现在我从未见过有外国钥匙的学生。 (除非是交换学生女朋友的公寓。)也不是书。所以“外键”不是模型的一部分。
Here's a model:
这是一个模型:
public interface Person {
String getName();
}
public interface Borrower<T> {
void borrow(T thing);
}
public interface Replacer<T> {
void replace (T thing);
}
public class Student implements Person,
Borrower<Book>, Replacer<Book> {
private final Set<Book> bookshelf = new HashSet<>();
public Collection<Book> getBookshelf() {
return Collections.unmodifiableSet(bookshelf);
}
// more implementation
}
I purposely decomposed the model into single-abstract-method (SAM) interfaces. The Person
interface would not be one typically, though. The action methods, transitive verbs like "borrow" and "replace", should be SAM interfaces, in part to support lambdas, in part to keep your model clear and simple to work with.
我故意将模型分解为单抽象方法(SAM)接口。但是Person接口通常不是一个。动作方法,像“借用”和“替换”这样的及物动词应该是SAM接口,部分是为了支持lambda,部分是为了让你的模型清晰易懂。