Architectural patterns

Open Source & DOTNET platform

Understanding architectural design patterns (like MVC, MVP, MVVM etc.) is essential for producing a maintainable, clean, extendable and testable source code. MVC MVC stands for Model-View-Controller. It is a which was introduced in 1970s. Also, MVC pattern forces a separation of concerns, it means domain model and controller logic are decoupled from (view). As a result maintenance and testing of the application become simpler and easier. MVC design pattern splits an application into three main aspects: Model, View and Controller MVC Model The Model represents a set of classes that describe the business logic i.e. business model as well as data access operations i.e. data model. It also defines business rules for data means how the data can be changed and manipulated.

View The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the controller as the result. This also transforms the model(s) into UI.

Controller The Controller is responsible to process incoming requests. It receives input from users via the View, then process the user's data with the help of Model and passing the results back to the View. Typically, it acts as the coordinator between the View and the Model. MVP This pattern is similar to MVC pattern in which controller has been replaced by the presenter. This design pattern splits an application into three main aspects: Model, View and Presenter.

This pattern is commonly used with ASP.NET Web Forms applications which require to create automated unit tests for their code-behind pages. This is also used with windows forms. MVP Model The Model represents a set of classes that describes the business logic and data. It also defines business rules for data means how the data can be changed and manipulated. View The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the presenter as the result. This also transforms the model(s) into UI. Presenter The Presenter is responsible for handling all UI events on behalf of the view. This receive input from users via the View, then process the user's data with the help of Model and passing the results back to the View. Unlike view and controller, view and presenter are completely decoupled from each other’s and communicate to each other’s by an interface. Also, presenter does not manage the incoming request traffic as controller. Key Points about MVP Pattern: User interacts with the View. There is one-to-one relationship between View and Presenter means one View is mapped to only one Presenter. View has a reference to Presenter but View has not reference to Model. Provides two way communication between View and Presenter. MVVM

MVVM stands for Model- View-View Model. This pattern supports two-way data binding between view and View model. This enables automatic propagation of changes, within the state of view model to the View. Typically, the view model uses the observer pattern to notify changes in the view model to model. MVVM Model The Model represents a set of classes that describes the business logic and data. It also defines business rules for data means how the data can be changed and manipulated. View The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the controller as the result. This also transforms the model(s) into UI.. View Model The View Model is responsible for exposing methods, commands, and other properties that helps to maintain the state of the view, manipulate the model as the result of actions on the view, and trigger events in the view itself.

This pattern is commonly used by the WPF, Silverlight, Caliburn, nRoute etc. Key Points about MVVM Pattern: User interacts with the View. There is many-to-one relationship between View and ViewModel means many View can be mapped to one ViewModel. View has a reference to ViewModel but View Model has no information about the View. Supports two-way data binding between View and ViewModel. Open Source Development Framework for developing web applications

Using Spring MVC Web Development Framework There are a majority of enterprises running Java applications and working on Java web development framework. What remains to be seen is that a number of companies are tied to the conventional web development framework and haven’t actually started to anticipate what could be the best Java web development framework. 10 best Java web development framework 1. Struts 2 2. JSF (Java Server Faces) 3. Spring MVC 4. Wicket 5. 6. Tapestry 7. RIFE 8. Seam 9. (GWT) 10. OpenXava Java Web Development Frameworks

“Full-stack” frameworks: SEAM, RIFE, Spring (?) Address back-end soup-to-nuts web development Typically include MVC component AND integration with enterprise systems ADVANTAGES: complete stack, limited glue code DISADVANTAGES: less plug-n-play; “heavyweight”

MVC frameworks: Struts, Spring MVC, etc. Address “page flow” and reusability in web development Focused on simplifying MVC-based Limited out-of-the-box integration with “enterprise” ADVANTAGES: pluggable architecture, “lightweight” DISADVANTAGES: glue code Choosing an MVC Framework Two types of MVC frameworks: • Action-based (aka Push-based MVC)  Data is pushed from controller to view  Focused more on request flow  Struts, Spring MVC, Stripes

• Component-based (aka Pull-based MVC)  Data is pulled in view  Focused more on view rendering  JSF, Wicket, Tapestry Spring MVC & Struts 1)Struts is a while spring is an application framework in which spring MVC is one of the modules of .

2)Spring is a Layered Architecture while Struts is not.

3)Struts is heavy weight while Spring is light weight.

4)Struts supports tag Library while Spring does not.

5)Spring is loosely coupled while Struts is tightly coupled.

6)Spring provides easy integration with ORM technologies while in struts, we need to do coding manually.

7)Struts easily integrate with other client side technologies. It is not easy in case of spring. Spring MVC Architecture

Front Controller design pattern

This design pattern enforces a single point of entry for all the incoming requests. All the requests are handled by a single piece of code which can then further delegate the responsibility of processing the request to further application objects. Spring MVC Architecture

MVC design pattern

This design pattern helps us develop loosely coupled application by segregating various concerns into different layers. MVC design pattern enforces the application to be divided into three layers, Model, View and Controller. Spring MVC Architecture Spring’s MVC module Spring’s MVC module is based on front controller design pattern followed by MVC design pattern. All the incoming requests are handled by the single servlet named DispatcherServlet which acts as the front controller in Spring’s MVC module. The DispatcherServlet then refers to the HandlerMapping to find a controller object which can handle the request. DispatcherServlet then dispatches the request to the controller object so that it can actually perform the business logic to fulfil the user request. (Controller may delegate the responsibility to further application objects known as service objects). The controller returns an encapsulated object containing the model object and the view object (or a logical name of the view). In Spring’s MVC, this encapsulated object is represented by class ModelAndView. In case ModelAndView contains the logical name of the view, the DispatcherServlet refers the ViewResolver to find the actual View object based on the logical name. DispatcherServlet then passes the model object to the view object which is then rendered to the end user. Spring MVC Architecture Spring’s MVC module Open Source Project Development Framework To build upon a basic project development framework for NIC, Uttar Pradesh so that any project development team can develop an application with less effort and in a structured and efficient manner. The framework shall be used as a basic template for development of web applications in open source framework. The framework is a direction and not binding for the development by different groups. It is open for upgradation and improvement based on the inputs received from various divisions/groups of NIC and never shall be thought of as a frozen or proprietary thing.

1. Controller 2. View (JSP pages) OSPDF 7 Layers: 3. DTO (Data Transfer Objects) (Document) 4. Service 5. Utilities 6. Entities 7. DAO (Data Access Object) Open Source Project Development Framework

User Request (request URL) DispatcherServlet (Front Controller)

Response back to User HandlerMapping

PersonController [stores form entries to DTO] through PersonService(DTO)

PersonDAO Utility Class PersonService Calls GenericDAO for CRUD for [Converts DTO to Entity for operations using Hibernate. Authentications WRITE and Entity to DTO for , validations, READ] conversions etc. calls PersonDAO(Entity) OSPDF using Spring MVC Development Environment

For project development using Spring MVC the following things are required:

IDE Eclipse: Eclipse is an integrated development environment (IDE) for Java and other programming languages like C, C++, PHP, and Ruby etc. Development environment provided by Eclipse includes the Eclipse Java development tools (JDT) for Java, Eclipse CDT for C/C++, and Eclipse PDT for PHP, among others.

Application Server JBoss AS 8.0: The JBoss applications server is a J2EE platform for developing and deploying enterprise Java applications, Web applications and services, and portals. J2EE allows the use of standardized modular components and enables the Java platform to handle many aspects of programming automatically. OSPDF using Spring MVC Development Environment PostgreSQL 9.4: PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness. PostgreSQL runs on all major operating systems, including Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows.

Development Framework Spring 4.0.3: Spring is a lightweight framework. The Spring framework comprises several modules such as IOC, AOP, DAO, Context, ORM, WEB MVC etc

Entity Framework Hibernate 4.0.1: Hibernate is a high-performance Object/Relational persistence and query service which is licensed under the open source GNU Lesser General Public License (LGPL) and is free to download. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities. Hibernate Hibernate is an Object-Relational Mapping (ORM) solution for JAVA. It is a powerful, high performance object/relational persistence and query service. It allows us to develop persistent classes following object-oriented idiom – including association, inheritance and polymorphism.

1.Hibernate is database independent. You can work with any database you want like oracle,mysql,db2,sql server ,etc. Using hibernate you won't worry about writing database specific queries and syntax. It's provides HQL (Hibernate Query Language), which is compatible with any database server. You just need to write queries in HQL, at the end hibernate converts HQL to underlying database and executes it.

2. In ORM, you will map a database table with java object called "Entity". So once you map these, you will get advantages of OOP concepts like inheritance, encapsulation, etc.

3. Caching mechanism (1st level & 2nd level cache) provided hibernate means you don't need to hit database for similar queries, you can cache it and use it from buffered memory to improve performance.

4.Supports Lazy loading (also called n+1 problem in Hibernate). Take an example-parent class has n number of child class. So When you want information from only 1 child class,there is no meaning of loading n child classes.This is called lazy loading (Load only thing which you want). I R E C T O R Y

S T R U C T U R E

Spring MVC Login Page

Sample codes for development using Spring MVC If you browse a URL http://10.135.2.177:8080/login

1. Layer-1: On seeing login, it will look for login controller (loginController.java) @RequestMapping(value = "/login", method = RequestMethod.GET) public ModelAndView login(@ModelAttribute("log") LoginDTO dto, @RequestParam(value = "error", required = false) String error, @RequestParam(value = "logout", required = false) String logout) { ………………………………………………. model.setViewName("login"); model.addObject("log", dto); return model; } 2 Layer-2: Controller has set the view name and the DTO (login.jsp) password: Sample codes for development using Spring MVC

3. Layer-3: Now we see LoginDTO.java file public class LoginDTO { public LoginDTO(){ private String loginname; private String username; public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; }

4. Layer-4 : Service implementation goes like this, (LoginSeviceImpl.java) public class LoginServiceImpl implements LoginService { public String getLoginName(String username){ return loginDao.getLoginName(username); } @Override public String getLoginPassword(String username){ return loginDao.getLoginName(username); } Sample codes for development using Spring MVC 5. Layer-5 : Data access object (LoginDao.java) public class LoginDaoImpl implements LoginDao { public String getLoginName(String username){ String encPass=""; Connection con=null;

6. Layer-6: Login entity (LoginUser.java) public class LoginUser implements UserDetails { private static final long serialVersionUID = 1L; private String username; private String password; @Override public String getUsername() { return username; }

7. Layer-7: General utilities (LoginUser.java) public class ConvertDTO2Entity {

public static User getEntity(PersonDTO dto){ User person=new User(); BeanUtils.copyProperties(dto, person); AJAX (Asynchronous JavaScript And XML) It is a group of inter-related technologies like JavaScript, DOM, XML, HTML, CSS etc. AJAX allows you to send and receive data asynchronously without reloading the web page.

XMLHttpRequest (XHR) is an API that can be used by JavaScript, JScript, VBScript, and other web browser scripting languages to transfer and manipulate XML data to and from a webserver using HTTP, establishing an independent connection channel between a webpage's Client-Side and Server-Side.

The Document Object Model (DOM) is a cross-platform and language-independent application programming interface that treats an HTML, XHTML, or XML document as a free structure wherein each node is an object representing a part of the document. MVC with AngularJS AngularJS is a JavaScript framework to create SPA (Single Page Application). It is a client side MV* (MVC+MVVM=MV*) framework. AngularJS extends HTML by providing Directives that add functionality to your markup and that allow you to create powerful dynamic templates for application. The following are some AngularJS directives which I have used in sample application. ng-app: It is entry point of AngularJS to the page. It shows that the application is AngularJS app. ng-controller: It is directive to define the controller. It attaches a controller class to the view. ng-repeat: It creates loop in your page like foreach loop. .module: It is use to create, register and retrieve all angular module. $http: It is an XMLHttpRequest object for requesting external data. $http.get: It read the json data. It takes two parameter url and config but config is optional. Example

(MVC 5 application development in DOTNET platform)

MVC application development using Visual studio 2013 is so simple and configurable, this is shown in the example. MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development MVC 5 application development The End