200 likes | 359 Views
MVC on the web 1.1 with figures from Christian Gross’ book, Ajax Patterns and Practices. Design Patterns. The hard problem in object-oriented (O-O) programming is deciding what objects to have, and what their responsibilities are
E N D
MVC on the web1.1with figures from Christian Gross’ book, Ajax Patterns and Practices
Design Patterns • The hard problem in object-oriented (O-O) programming is deciding what objects to have, and what their responsibilities are • Design Patterns describe recommended designs for common problems • Design patterns are a current hot topic in O-O design • The book which started it all is described here: http://en.citizendium.org/wiki/Design_pattern
why Model-View-Controller designs? • in 1870, someone wrote a novel • there is a 2002 audio-tape version of it • there is a 1926 hardback edition of it • there is a 2005 paperback edition of it • in 1987, a film was made of it • one novel – but 4 different ways of presenting it
Model-View-Controller pattern goals • there are often many ways to present the same problem • requirements on how a problem is displayed tend to change (i.e., “experimentation” goes on) • the MVC design tries to separate the code that represents the problem from the code that presents the problem to the user • this allows the “presentation” part to be change more easily • this “pattern” arose because management has so often demanded that programmers modify presentation • without the correct design, that’s “hard” • with this pattern, it becomes much “easier”
About the MVC pattern • Model View Controller • separation of concerns • content (data) is the Model • data may be derived from multiple sources • there are potentially multiple ways of presenting the content • each presentation method is one View • the Controller is the code that (possibly based on user parameters) gets data, from (possibly multiple) sources, and aggregates it into an internal representation • a View takes an internal representation of some data and presents it in the currently appropriate way • based on browser type or device type, for example
An example - Google • Three different browser technologies • Graphical User Interface (IE, Firefox, Safari, Opera, etc.) • Text-based browser • Wireless Access Protocol (WAP) browser • on cell phones
generic URL’s • This is what most developers do: • GET /bankaccount/login.jsp • GET /bankaccount/login.aspx • GET /bankaccount/login.php • This might (ideally) be better: (platform independent) • GET /bankaccount/login • But, how could we get the generic URL to go to the current implementation?
Resource vs. Representation • Resource: • GET /bankaccount/login • Representation • GET /bankaccount/login.jsp • GET /bankaccount/login.aspx • GET /bankaccount/login.php
which database shall we use? • what if we want to stop using one database and start using another? • do we need to change the website source code (and maybe recompile)? • the recommended way is to decouple details about where the data is from the logic which uses the data • traditionally, by web.config files (Java or .NET) • Ruby does this a little differently • using a web.config is a form of dependency injection
dependency injection • a contract between an object and the environment • The object agrees not to go out searching for the resources it needs, partners it collaborates with, or services it uses. • Instead, the object provides a means for these dependencies to be provided to it. • In turn, the execution environment agrees to provide the object with all of the dependencies it needs, before the object needs them.
Interfaces in OO languages (a form of runtime binding) class Factory { public static Ibase Instantiate() { return new Implementation2(); } } class Client { void UseIt() { Ibase obj = Factory.Instantiate(); obj.DoSomething(); } } interface Ibase { void DoSomething(); } class Implementation1 : IBase { void DoSomething() { // code here } } class Implementation2 : IBase { void DoSomething() { // code here } }
web servers and root URL’s • all web servers implement the separation of resource from representation for the root URL • “default documents” could be one of many, such as: • index.html • index.jsp • index.pgp • default.aspx
for specific web “applications” • you may need to implement a filter to do URL rewriting • resource: • http://harbormist.com/bankaccount/login • a filter would rewrite the URL as one of: • http://harbormist.com/bankaccount/login.jsp • http://harbormist.com/bankaccount/login.aspx • http://harbormist.com/bankaccount/login.php
example code for a web server filter • Ajax Patterns and Practices, chapter 5, p. 128 • shows how to write an application filter in ASP.NET • an equivalent method exists on Apache web servers