Staram się zrozumieć wzór MVC. Pracowałem z frameworkami MVC, takimi jak ASP.NET MVC i Django, ale struktura projektu jest w dużym stopniu wymuszona, więc naprawdę nie pomogło mi zrozumieć, jak tworzyć własne aplikacje oparte na tym wzorcu. Aby to wyjaśnić postanowiłem napisać najprostszy przykład mojego zrozumienia MVC (program konsoli w Pythonie) i dowiedzieć się, czy coś jest nie tak.MVC najprostszy przykład
|- program:
|—— controller.py
|—— model.py
|—— view.py
|—— db.txt #simulates database
To jest moja podstawowa struktura. To, co zrobi ten program, to wyświetlenie wszystkich osób znajdujących się w pliku db.txt. Używam db.txt (json) do symulacji rzeczywistej bazy danych.
controller.py
from model import Person
import view
def showAll():
#gets list of all Person objects
people_in_db = Person.getAll()
#calls view
return view.showAllView(people_in_db)
def start():
view.startView()
input = raw_input()
if input == 'y':
return showAll()
else:
return view.endView()
if __name__ == "__main__":
#running controller function
start()
view.py
from model import Person
def showAllView(list):
print 'In our db we have %i users. Here they are:' % len(list)
for item in list:
print item.name()
def startView():
print 'MVC - the simplest example'
print 'Do you want to see everyone in my db?[y/n]'
def endView():
print 'Goodbye!'
model.py
import json
class Person(object):
def __init__(self, first_name = None, last_name = None):
self.first_name = first_name
self.last_name = last_name
#returns Person name, ex: John Doe
def name(self):
return ("%s %s" % (self.first_name,self.last_name))
@classmethod
#returns all people inside db.txt as list of Person objects
def getAll(self):
database = open('db.txt', 'r')
result = []
json_list = json.loads(database.read())
for item in json_list:
item = json.loads(item)
person = Person(item['first_name'], item['last_name'])
result.append(person)
return result
więc jest to scenariusz WH en użytkownik chce widzieć wszystkie osoby w bazie danych:
Czy to podejście jest prawidłowe?